Sign in to follow this  
Dv02

std::vector as argument

Recommended Posts

I have been trying to explore my knowledge, however on this i am stumped. .:The Idea:. To pass a vector array into a function .:The Current Code:.
#include<iostream>
#include<vector>

using namespace std;

void cvector(vector<int> &hammer())
{
     for(int i=0;i==2;i++){
     cout<<&hammer[i]<<endl;
     }
}

int main()
{
    vector<int> stuff;
    stuff.push_back(1);
    stuff.push_back(2);
    stuff.push_back(3);
    cvector(stuff);
    
    cin.get();
    return 0;

}


Very Simple. .:The Problem:. Nothing comes up. It compiles but the program doesnt appear. task Manager doesnt even show it processing. Compiler:Dev-Cpp I have searched a few threads and saw a bit more advanced problem, but not like this one. Awaiting reply.

Share this post


Link to post
Share on other sites

void cvector(vector<int> &hammer())
{
for(int i=0;i==2;i++){
cout<<&hammer[i]<<endl;
}
}


What's the () for after hammer?

How could i == 2 if you just set it to 0?

Step through it in a debugger, you'll be able to debug your program that way.

Share this post


Link to post
Share on other sites
[quote]Original post by RDragon1

void cvector(vector<int> &hammer())
{
for(int i=0;i==2;i++){
cout<<&hammer[i]<<endl;
}
}

Quote:

What's the () for after hammer?


good point.

Quote:

How could i == 2 if you just set it to 0?


because i==2 is testing if it 2 or not. since it is initialized to 0 it would have to make 3 passes through the for(;;) loop.

Ran it through a debugger, and i am pretty sure it didnt see anything wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dv02
Quote:

How could i == 2 if you just set it to 0?


because i==2 is testing if it 2 or not. since it is initialized to 0 it would have to make 3 passes through the for(;;) loop.


Exactly. It's initialized to 0. So before it enters the loop, it tests if i == 2, which is false, so it never enters the loop at all.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Quote:
Original post by Dv02
Quote:

How could i == 2 if you just set it to 0?


because i==2 is testing if it 2 or not. since it is initialized to 0 it would have to make 3 passes through the for(;;) loop.


Exactly. It's initialized to 0. So before it enters the loop, it tests if i == 2, which is false, so it never enters the loop at all.


I guess before i posted i should have changed it to i!=2. Sorry. Well, i put that in there and same problem. compiles fine, but i dont get a console or visual. also, thx for the lesson

Share this post


Link to post
Share on other sites
firstly, use vector::size();

for(int i = 0; i < myVector.size(); i++)
{

}


Secondly, try doing:

void cvector(vector<int> &hammer)
{
for(int i=0;i<hammer->size();i++){

cout<<hammer->at(i)<<endl;

}
}


I havent tested this at all fyi

Share this post


Link to post
Share on other sites

#include<iostream>
#include<vector>

using namespace std;

void cvector(const vector<int> &hammer) // no need for (), added const since it will not be modified
{
for( int i=0; i != hammer.size() ; ++i ){ // check for vec size, not magic numbers, use preincrement when postincrement not needed (forum might not display "plusplus" before i
cout<<hammer[i]<<endl; // '&' in front of hammer[i] will give you the address of the variable, not the var itself
}
}

int main()
{
vector<int> stuff;
stuff.push_back(1);
stuff.push_back(2);
stuff.push_back(3);
cvector(stuff);

cin.get();
return 0;

}



Share this post


Link to post
Share on other sites
Quote:
Original post by Crazyfool
firstly, use vector::size();

for(int i = 0; i < myVector.size(); i++)
{

}


Secondly, try doing:

void cvector(vector<int> &hammer)
{
for(int i=0;i<hammer->size();i++){

cout<<hammer->at(i)<<endl;

}
}


I havent tested this at all fyi


only error i get is
8 base operand of `->' has non-pointer type `std::vector<int, std::allocator<int> >'

EDIT

I got it to go back to the origional no show by using periods instead of the ->

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this