C++ showing vectors problem

Started by
15 comments, last by rip-off 12 years, 8 months ago
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:



vector<string> inventory;

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




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



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




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


Thank you!
Advertisement

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:



vector<string> inventory;

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




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



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




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


Thank you!


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



class Foo
{
protected:
vector<string> stuff;

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

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

};

Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
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.
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.
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)
Hello


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.

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)

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)
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.

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);


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.
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).




// 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] // createsinventory !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[font="Times New Roman"] [/font] vector<string> inventory;
[font="Times New Roman"] [/font] inventory.push_back("sword");
[font="Times New Roman"] [/font] inventory.push_back("shield");
[font="Times New Roman"] [/font] inventory.push_back("armor");

[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(move == "inventory")
[font="Times New Roman"] [/font] {
[font="Times New Roman"] [/font] ++day;
[font="Times New Roman"] [/font] for(int i = 0; i < inventory.size(); ++i)
[font="Times New Roman"] [/font] cout inventory;
[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.
move:

vector<string> inventory;

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

like so:

using namespace std;
vector<string> inventory;


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

This topic is closed to new replies.

Advertisement