Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

Vector Help [ Solved ]

This topic is 4420 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

Hello, I tried passing a vector's values onto another function, but i generated an error, can someone please help me out. + Any help is greatly Appreciated
void trash()
{

    vector<string> diaper;
    diaper.push_back("Smelly");
    diaper.push_back("WOW!");

}

void poop( vector<string> diaper )
{
     
    // Function code here... 

}




[Edited by - NUCLEAR RABBIT on June 10, 2006 1:57:55 AM]

Share this post


Link to post
Share on other sites
Advertisement
errr... are you trying to call poop(diaper) from somewhere? What's your exact error?

Also, unless it's highly important that you copy all the values of your vector, I recommend you pass by reference:

void poop( const vector<string> &diaper ) {}

Much faster, since you're really just passing a memory reference.

Share this post


Link to post
Share on other sites
Im trying to use the values of diaper in another function.

Ex:

void trash()
{

vector<string> diaper;
diaper.push_back("Smelly");
diaper.push_back("WOW!");

}

void poop( vector<string> diaper )
{

for(int i = 0; i < 2; i++)
{
cout << diaper << endl;
}

}

Share this post


Link to post
Share on other sites
Again, it would help if you told what the error was. I coded up a quick test app. for your functions and have no problem. I have a main function actually calling one of the functions, but other than that there's not a whole lot majorly different. You could change this to use iterators on the vector as well, but that's not the main point of this exercise anyway I imagine.


#include <vector>
#include <string>
#include <iostream>

void trash();
void poop(const std::vector<std::string> &diaper);

int main ( void )
{
trash();
return 0;
}


void trash()
{
std::vector<std::string> diaper;
diaper.push_back("Smelly");
diaper.push_back("WOW!");

poop(diaper);

}

void poop(const std::vector<std::string> &diaper )
{
for(int i = 0; i < 2; i++)
{
std::cout << diaper << std::endl;
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by caffeineaddict
Again, it would help if you told what the error was. I coded up a quick test app. for your functions and have no problem. I have a main function actually calling one of the functions, but other than that there's not a whole lot majorly different. You could change this to use iterators on the vector as well, but that's not the main point of this exercise anyway I imagine.

*** Source Snippet Removed ***


Hey man, thanx for the help. I got it to work [smile]

Share this post


Link to post
Share on other sites
Also note that this works:

#include <iostream> //for the basic commands(string, cout, etc...)
#include <vector> //For the vectors
using namespace std; //Because I'm lazy and don't want to type 'std::' before strings/cout/endl/cin/etc...

vector<string> diaper;
//Declaring 'diaper' outside of any functions make it a global variable(as oposed to a
//local one) This allows any function to have access to it.

void trash(), poop(); //Pre-initialization of the functions 'trash' and 'poop'

int main()
{
trash(); //Use function 'trash'
poop(); //Use function 'poop'
cin.get(); //Wait for input before exiting
return 0; //exit
}

void trash() //The function itself
{
diaper.push_back("Smelly");
diaper.push_back("WOW!");
}

void poop()//The other function
{
for(int i = 0; i < 2; i++)
{
cout << diaper << endl;
}
}


You can't alter a varaible outside of a function, but you can intialize one. Doing so makes it a global and lets any function(on this file) access it.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!