Sign in to follow this  
ndrul

C++ showing vectors problem

Recommended Posts

Hello everybody, this is my first post so let me know if I do anything wrong.


Anyways, I am teaching myself C++ with "Beginning C++ game programming" (Dawson). I have actually found it rather easy so far (chapter 8 and 1 1/2 months). Now I am trying to finally branch out a bit on my own and have encountered a problem.



I am makng a text RPG and am trying to make the inventroy with a vector. I declare with the following code:

[code]

vector<string> inventory;

inventory.push_back("sword");
inventory.push_back("shield");
inventory.push_back("armor");

[/code]


This compiles fine, so I assume it is correct. But two functions later, I use this code to display the inventory:

[code]

for (int i = 0; i < inventory.size(); ++i)
cout << inventory[i];

[/code]


I use microsoft visual 2010 express and it says "error C2065: 'inventory' : undeclared identifier". What have I done wrong?


Thank you!

Share this post


Link to post
Share on other sites
[quote name='ndrul' timestamp='1313099787' post='4847951']
Hello everybody, this is my first post so let me know if I do anything wrong.


Anyways, I am teaching myself C++ with "Beginning C++ game programming" (Dawson). I have actually found it rather easy so far (chapter 8 and 1 1/2 months). Now I am trying to finally branch out a bit on my own and have encountered a problem.



I am makng a text RPG and am trying to make the inventroy with a vector. I declare with the following code:

[code]

vector<string> inventory;

inventory.push_back("sword");
inventory.push_back("shield");
inventory.push_back("armor");

[/code]


This compiles fine, so I assume it is correct. But two functions later, I use this code to display the inventory:

[code]

for (int i = 0; i < inventory.size(); ++i)
cout << inventory[i];

[/code]


I use microsoft visual 2010 express and it says "error C2065: 'inventory' : undeclared identifier". What have I done wrong?


Thank you!
[/quote]

Where is the vector declared? If it is a class member, you should declare it in the class, not inside that function.

[code]

class Foo
{
protected:
vector<string> stuff;

private:
void fillStuff()
{
stuff.push_back("thing1");
// etc ...
}

void displayStuff()
{
// iterate and do stuff.
}

};

[/code]

Share this post


Link to post
Share on other sites
Need to see more code to know exactly why, but the place you are running that loop, does not have "inventory" in its scope, so it cant "see it" and therefore cant print its contents.

Share this post


Link to post
Share on other sites
Sorry. Inventory is declared int the main() function. Then comes the game loop() function. If the player inputs "inventory", then it goes to the showInventory(). That is where i try to display it.



I would post the whole code, but i dont think it is very clean.

Share this post


Link to post
Share on other sites
But the idea of putting it inside the player class is a good idea. Thank you.

Do you have any problem with me doing that (I'll assume not)

Share this post


Link to post
Share on other sites
Hello

[quote name='NumberXaero' timestamp='1313099995' post='4847953']
Need to see more code to know exactly why, but the place you are running that loop, does not have "inventory" in its scope, so it cant "see it" and therefore cant print its contents.
[/quote]
No want to be a parrot, but this is exactly that :).
Please could you post the whole code ?

(The problem is that your vector 'inventory' is out of the scope of your display function. You are declaring it at a wrong place)

Share this post


Link to post
Share on other sites
[quote]
No want to be a parrot, but this is exactly that :).
Please could you post the whole code ?

(The problem is that your vector 'inventory' is out of the scope of your display function. You are declaring it at a wrong place)
[/quote]


Ok. I will post it as soon as I get back to my computer ( around 9 I hope)

Share this post


Link to post
Share on other sites
This won't solve your problem, but I use the .At( ) operator of vectors, it makes debugging a little bit easier for me.
But all that aside I the only difference is at() sends a out_of_range exception if something is out of range, handy if you ask me.

[code]vector<string> inventory;

inventory.push_back("sword");
inventory.push_back("shield");
inventory.push_back("armor");


for (int i = 0; i < inventory.size(); ++i)
cout << inventory.at(i);
[/code]

edit: also in this example you actually can't get an out of range exception =p
Unless you change the size of inventory inside the loop, if your doing that you should reset your iterator anyway.

Share this post


Link to post
Share on other sites
Here is the code. DO NOT run it. I had to copy it to word first, so your compiler might break if you try (partly joking).



[quote]
// txtrpg.cpp : main project file.
[font="Times New Roman"] [/font]#include "stdafx.h"
[font="Times New Roman"] [/font]#include <iostream>
[font="Times New Roman"] [/font]#include <string>
[font="Times New Roman"] [/font]#include <vector>

[font="Times New Roman"] [/font]// myfiles
[font="Times New Roman"] [/font]#include "playerClass.h"
[font="Times New Roman"] [/font]#include "enemyClass.h"

[font="Times New Roman"] [/font]using namespace std;

[font="Times New Roman"] [/font]//functions
[font="Times New Roman"] [/font]voidtellStory();
[font="Times New Roman"] [/font]voidgameloop();
[font="Times New Roman"] [/font]voidinstructions ();
[font="Times New Roman"] [/font]voidmerchant();
[font="Times New Roman"] [/font]voidtrain();
[font="Times New Roman"] [/font]voidhire();

[font="Times New Roman"] [/font]// main tells story and starts game loop

[font="Times New Roman"] [/font]int main()
[font="Times New Roman"] [/font]{
[font="Times New Roman"] [/font] tellStory();

[font="Times New Roman"] [/font] // [b]createsinventory[/b] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[font="Times New Roman"] [/font] vector<[b]string[/b]> [b]inventory[/b];
[font="Times New Roman"] [/font] [b]inventory[/b].push_back[b]("[/b]sword");
[font="Times New Roman"] [/font] inventory.push_[b]back[/b]("[b]shield[/b]");
[font="Times New Roman"] [/font] inventory.[b]push[/b]_back("[b]armor[/b]");

[font="Times New Roman"] [/font] // ready tobegin?
[font="Times New Roman"] [/font] string ready;
[font="Times New Roman"] [/font] string* pReady = &ready;
[font="Times New Roman"] [/font] do
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] cout << "\n\nAre you ready to begin? (yes/no) \n\n";
[font="Times New Roman"] [/font] cin >> *pReady;

[font="Times New Roman"] [/font] } while(ready != "yes");

[font="Times New Roman"] [/font] gameloop();

[font="Times New Roman"] [/font] cout << "youmust now fight the dragon";

[font="Times New Roman"] [/font] char f;
[font="Times New Roman"] [/font] cin >> f;
[font="Times New Roman"] [/font] return 0;
}

[font="Times New Roman"] [/font]// tellsstory
[font="Times New Roman"] [/font]voidtellStory()
[font="Times New Roman"] [/font]{

[font="Times New Roman"] [/font] // tell story
[font="Times New Roman"] [/font] cout << "Thereonce was a nation of great power and might. It was called Costinole, and it wasthe most powerful country of all time. ";
[font="Times New Roman"] [/font] cout << "100Costinolean soldiers could defeat an army of 1,000. The ruler of Costinole wasKing Ostincise. ";
[font="Times New Roman"] [/font] cout << "Hewas a great warrior and had ruled Costinole since its creation. He ledCostinole with an iron fist and sage wisdom. ";
[font="Times New Roman"] [/font] cout << "Oneday, a new threat arrived in Costinole : a dragon. Costinole had never fought adragon before. ";
[font="Times New Roman"] [/font] cout << "Thefinest knights were sent and three days later, all were dead. Many more knightswere sent to the same fate. ";
[font="Times New Roman"] [/font] cout << "Eventually,King Ostincise went with a group of elite knights. None of them ever returned.";
[font="Times New Roman"] [/font] cout << "Afterthe death of its leader, Costinole fell apart. Since then, it has been waitingfor a great warrior to arise and lead it back to greatness. ";
[font="Times New Roman"] [/font] cout << "Manyhave tried, and many have failed. Are you up to the task.? \n\n\n";

[font="Times New Roman"] [/font] // giveinstruction
[font="Times New Roman"] [/font] cout << "Youare the latest young soldier to try and fulfill this task. In order to proveyourself, you have been given 100 days to prove yourself. ";
[font="Times New Roman"] [/font] cout << "Duringthis time, you have a number of tasks you may do to keep yourself busy. On thehundredth day, you must defeat the dragon. ";
[font="Times New Roman"] [/font] cout << "Inorder to prepare yourself, you can train, fight enemies, buy items, and hireallies. ";
[font="Times New Roman"] [/font] cout << "Forfull instructions, insert 'instructions'. ";
[font="Times New Roman"] [/font]}

[font="Times New Roman"] [/font]// gameloop is the game loop
[font="Times New Roman"] [/font]voidgameloop()
[font="Times New Roman"] [/font]{
[font="Times New Roman"] [/font] for (int day = 1; day < 100;)
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] cout<< endl << endl << "Day" << day << endl;

[font="Times New Roman"] [/font] string move;
[font="Times New Roman"] [/font] cin >> move;

[font="Times New Roman"] [/font] if(move == "instructions")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] instructions();
[font="Times New Roman"] [/font] }

[font="Times New Roman"] [/font] if(move == "fight")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] ++day;
[font="Times New Roman"] [/font] cout << "fight";
[font="Times New Roman"] [/font] }

[font="Times New Roman"] [/font] if(move == "merchant")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] ++day;
[font="Times New Roman"] [/font] merchant();
[font="Times New Roman"] [/font] }

[font="Times New Roman"] [/font] if(move == "train")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] ++day;
[font="Times New Roman"] [/font] train();
[font="Times New Roman"] [/font] }

[font="Times New Roman"] [/font] if(move == "hire")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] ++day;
[font="Times New Roman"] [/font] hire();
[font="Times New Roman"] [/font] }
[font="Times New Roman"] [/font]
[font="Times New Roman"] [/font] if([b]move[/b] == "inventory")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] ++day;
[font="Times New Roman"] [/font] for(int i = 0; i < [b]inventory[/b].size(); ++i)
[font="Times New Roman"] [/font] cout [b]inventory[/b][i];
[font="Times New Roman"] [/font] }
[font="Times New Roman"] [/font] }
[font="Times New Roman"] [/font] return;
[font="Times New Roman"] [/font]}

[font="Times New Roman"] [/font]//instructions shows commands and info about stats
[font="Times New Roman"] [/font]voidinstructions()
[font="Times New Roman"] [/font]{

[font="Times New Roman"] [/font] //commands
[font="Times New Roman"] [/font] cout << "Typefight to fight enemies. Fighting gives you gold and experience."<< endl;
[font="Times New Roman"] [/font] cout << "Typetrain to train. Training allows you to increase a stat by 1."<< endl;
[font="Times New Roman"] [/font] cout << "Typemerchant to visit the merchant. You can buy equipment from the merchant."<< endl;
[font="Times New Roman"] [/font] cout << "Typehire to hire a soldier to fight with you. Soldiers will help you inbattle." << endl;
[font="Times New Roman"] [/font] cout << "Typeinventory to display your inventory" << endl;

[font="Times New Roman"] [/font] //stats
[font="Times New Roman"] [/font] cout << "\nYourmain stats are health, defense, and damage. Health is the amount of damage youcan take before death.";
[font="Times New Roman"] [/font] cout << "Usea health potion to receive some health. Damage is the amount of damage youdeal. Swords increase your damage.";
[font="Times New Roman"] [/font] cout << "Defenseis your damage resistance stat. Every 3 defense reduce the damage you receiveby 1.";
[font="Times New Roman"] [/font]}

[font="Times New Roman"] [/font]//merchant lets player buy and sell stuff
[font="Times New Roman"] [/font]voidmerchant()
[font="Times New Roman"] [/font]{
[font="Times New Roman"] [/font] cout << "Hello,what would you like to buy? I have swords, armor, shields, and potions";
[font="Times New Roman"] [/font]}

[font="Times New Roman"] [/font]//letsyou train
[font="Times New Roman"] [/font]voidtrain()
{
[font="Times New Roman"] [/font] cout << "train";
[font="Times New Roman"] [/font]}

[font="Times New Roman"] [/font]//letsyou hire help
[font="Times New Roman"] [/font]voidhire()
[font="Times New Roman"] [/font]{
[font="Times New Roman"] [/font] cout << “hire”;
[font="Times New Roman"] [/font]}
[/quote]


Sorry for all your trouble. I tried to bold stuff about the inventory, but im having technology trouble.

Share this post


Link to post
Share on other sites
move:

vector<string> inventory;

outside of your main scope to right after:
using namespace std;

like so:
[code]
using namespace std;
vector<string> inventory;[/code]

When you leave main() your functions cannot see your vector that's why your getting those errors.

Share this post


Link to post
Share on other sites
void gameloop() {} cannot print inventory because its defined in main() and you are trying to access it in gameloop().

[code]

// 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);
// ...
}

[/code]

also when posting code use the little <> button to format and wrap the code in code tags for reading

Share this post


Link to post
Share on other sites
I just tried that but now it is saying that i am re-declaring inventory.



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

Share this post


Link to post
Share on other sites
I'm really sorry to keep bothering everybody, but i just cant seem to figure this out. Now i get this error:



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

Share this post


Link to post
Share on other sites
[quote name='ndrul' timestamp='1313117865' post='4848076']
I just tried that but now it is saying that i am re-declaring inventory.



EDIT: That is when i tried yewbie's solution.
[/quote]

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Your reputation is (currently) linked to whether people "like" your posts. See [url="http://www.gamedev.net/topic/606208-negative-reputation/"]here[/url] for a recent discussion about reputation on this site.

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

Share this post


Link to post
Share on other sites

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