#1 Members - Reputation: 341
Posted 30 October 2012 - 10:12 PM
I made a vector of a string and added a member function of an item class getName, witch just returns the name:
[source lang="cpp"]string Item::getName() { return m_Name;}[/source]
so i initialized the vector and iterator like so:
[source lang="cpp"]vector<string> inventory;vector<string>::iterator iter;[/source]
and then:
[source lang="cpp"]inventory.push_back(sword.getName());[/source]
Now my question is how to print the names of the items. the item class:
[source lang="cpp"]class Item{public: enum Type{WEAPON, ARMOR, CONSUMABLE, TRASH}; Item(string name, Type type, int rarity, int value); string getName();private: Type m_Type; string m_Name; int m_Rarity; int m_Value;}; Item::Item(string name, Type type, int rarity, int value): m_Name(name), m_Type(type), m_Rarity(rarity), m_Value(value) {}string Item::getName() { return m_Name;}[/source]
Now what i want to do is make an inventory class that handles adding dropping, equipping, and listing all the items in the inventory. I have no idea how to print the names though. all help is appreciated.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#2 Marketplace Seller - Reputation: 8899
Posted 30 October 2012 - 10:29 PM
inventory.at(0); //Get the first string in the vector, or throw an exception if out of range. inventory[0]; //Get the first string in the vector, or hopefully crash if out of range. inventory.back(); //Get the last string in the vector, or hopefully crash if the vector is empty. inventory.front(); //Get the first string in the vector, or hopefully crash if the vector is empty.
(always check inventory.empty() or inventory.size() before accessing an element)
To print out everything:
By index:
for(unsigned int i = 0; i < inventory.size(); i++)
{
std::cout << inventory[i] << std::endl;
}With iterators:
typedef std::vector<std::string>::iterator StrVectorIterator;
for(StrVectorIterator myIt = inventory.begin(); myIt != inventory.end(); myIt++)
{
std::cout << (*myIt) << std::endl;
}Iterators are designed to be similar to pointers; you don't need to create an iterator until you actually need it - infact, the iterators will be invalidated if you operate on the vector after getting the iterator.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#3 Senior Moderators - Reputation: 3113
Posted 30 October 2012 - 10:40 PM
With C++11:... good stuff ...
for(auto const& val : inventory)
std::cout<<val<<std::endl;
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX
#4 Members - Reputation: 341
Posted 30 October 2012 - 10:58 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#5 Senior Moderators - Reputation: 3113
Posted 30 October 2012 - 11:07 PM
Servant, i tried both those ways but i get an error on the output operator saying operator "<<" matches these operands. i think i need to overload a the operator, but the book i learned from was kinda confusing when it taught that.
Did you make sure to include <string>?
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX
#6 Members - Reputation: 341
Posted 30 October 2012 - 11:11 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#7 Members - Reputation: 1548
Posted 31 October 2012 - 11:33 AM
std::vector<Item> Inventory;
// Add a sword ...is typical parameters
Item Sword(...);
Inventory.push_back(Sword);
// List your inventory
for (int i = 0; i < Inventory.size(); ++i) {
std::cout << Inventory[i].getName();
}
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)






