Public Group

# std::vector as argument

This topic is 3606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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<<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 on other sites
void cvector(vector<int> &hammer()){     for(int i=0;i==2;i++){     cout<<&hammer<<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 on other sites
Original post by RDragon1
void cvector(vector<int> &hammer()){     for(int i=0;i==2;i++){     cout<<&hammer<<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 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 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 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 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<<endl; // '&' in front of hammer 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 on other sites
Quote:
 Original post by Crazyfoolfirstly, use vector::size();for(int i = 0; i < myVector.size(); i++){}Secondly, try doing:void cvector(vector &hammer){ for(int i=0;isize();i++){ cout<at(i)<

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 on other sites
Thank you guys, my code now works.

also, Thanks for teaching me some lessons about vector.

##### Share on other sites
Good to hear, yea sorry my mind has been a bit fuzzy tonight ;p. glad to see you got it working.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632606
• Total Posts
3007383

×