Jump to content
  • Advertisement
Sign in to follow this  
Dv02

std::vector as argument

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 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<<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 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
Thank you guys, my code now works.

also, Thanks for teaching me some lessons about vector.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!