#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;
}
std::vector as argument
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:.
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.
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.
Original post by RDragon1void 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.
Quote:Original post by Dv02Quote:
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.
Quote:Original post by RDragon1Quote:Original post by Dv02Quote:
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
firstly, use vector::size();
Secondly, try doing:
I havent tested this at all fyi
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
#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;}
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 ->
Good to hear, yea sorry my mind has been a bit fuzzy tonight ;p. glad to see you got it working.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement