C++ showing vectors problem

Started by
15 comments, last by rip-off 12 years, 8 months ago
void gameloop() {} cannot print inventory because its defined in main() and you are trying to access it in gameloop().



// change gameloop to

void gameloop(std::vector<string>& inventory)
{
// loop and print
}

// use it in main as

void main()
{
std::vector<string> inventory;

//...
gameloop(inventory);
// ...
}



also when posting code use the little <> button to format and wrap the code in code tags for reading
Advertisement
I just tried that but now it is saying that i am re-declaring inventory.



EDIT: That is when i tried yewbie's solution.
I'm really sorry to keep bothering everybody, but i just cant seem to figure this out. Now i get this error:




[font="Consolas"][size="1"][font="Consolas"][size="1"]1>txt rpg.cpp(44): error C2660: 'gameloop' : function does not take 1 arguments

[/font][/font]
[/quote]
You didnt change the forward declaration for the gameloop function at the top of the file you just added it to the definition at the bottom.Change both, they have to match.

I just tried that but now it is saying that i am re-declaring inventory.



EDIT: That is when i tried yewbie's solution.


yewbies solution will work. It is putting the inventory variable in the global namepsace. The reason it fails is because you still declare it in main, you can only declare it once. Either in main, or in the global namespace.

The preferred way to do it is with parameter passing: gameloop(std::vector<> inventory) I recommend you look into it. It makes things much easier once you move to multiple headers where using the global namepsace like this become unmanageable.

The argument error is because you're still doing gameloop(inventory);

but your gameloop declaration is still gameloop() <- no arguments
Duh. Sorry about that. I changed it and now everything works. Thank you everybody. One last question. What is the reputation under your profile name?
Your reputation is (currently) linked to whether people "like" your posts. See here for a recent discussion about reputation on this site.

Having 0 reputation just means you are relatively new to the site.

This topic is closed to new replies.

Advertisement