Sign in to follow this  

Vector Help [ Solved ]

This topic is 4209 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
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[i] << 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[i] << 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[i] << 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
this isn't really related to vectors, but its more of a general coding tip:

you should really name your variables and functions meaningfully, even in short test cases like those youve posted(its always a good practice to get into). names like poop and diaper are not only uninforamtive, but if someone else is reading your code they may such names...less than tastefull.

[Edited by - Driv3MeFar on June 10, 2006 9:07:56 AM]

Share this post


Link to post
Share on other sites

This topic is 4209 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.

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