Vector Help [ Solved ]

Started by
9 comments, last by Driv3MeFar 17 years, 10 months ago
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]
Advertisement
I think you want:
string fname;cout << ""Friends Name: ";cin >> fname;friends.pushback(fname);
Servant,

Thanks for the Help man. [smile]

EDIT: Added new problem im having

[Edited by - NUCLEAR RABBIT on June 10, 2006 12:16:52 AM]
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.
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;    } }
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;    } }
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]
I'm curious now, what was wrong with it? :D
Also note that this works:
#include <iostream> //for the basic commands(string, cout, etc...)#include <vector> //For the vectorsusing 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.
Quote:Original post by Replicon
I'm curious now, what was wrong with it? :D


It wasn't global I think.
caffeineaddict dodged making it global by reading strait from it's address.

This topic is closed to new replies.

Advertisement