好的,我有一个inventory
类来访问我的基类MainShop
中的静态向量。我还有一个继承自SwordShop
的{{1}}类。 (Inventory和SwordShop都是派生类)。
MainShop
例如,假设用户将//SwordShop class
void SwordShop::Shop
{
//I have a setter function where it takes an integer and a string and adds its to my hashmap.
//Also have a getter function which has a for loop displaying my items in my map
this->setWeaponSoldier(1, "1) Meito Ichimonji\n +4 Damage\n 150Gold");
this->setWeaponSoldier(2, "2) Shusui\n +10 Damage\n 230Gold");
this->setWeaponSoldier(3, "3) Elixir\n +16 Damage\n 300Gold");
this->setWeaponSoldier(4, "4) Blade of scars\n +24 Damage\n 550Gold");
this->setWeaponSoldier(5, "5) Ragnarok\n +32 Damage\n 610Gold");
this->setWeaponSoldier(6, "6) Eternal Darkness\n +40 Damage\n 690Gold");
this->setWeaponSoldier(7, "7) Masamune\n +52 Damage\n 750Gold");
this->setWeaponSoldier(8, "8) Soul Calibur\n +60 Damage\n 900Gold");
}
//Function in my inventory class
void Inventory::DisplayInventory()
{
int choice;
cout << "\nWhat do you want to do?\n1) Check Status\n2) Equip Weapons\n";//Equip what is in your inventory
cin >> choice;
switch (choice)
{
case 1: this->DisplayStats();
break;
case 2:cout << WeaponInventory.size() << endl;//debug
if (!WeaponInventory.empty())//Make sure inventory is not empty
{
cout << "Your current Weapons are: \n";
for (unsigned int i = 0; i < WeaponInventory.size(); ++i)
cout << i+1 << ") " << WeaponInventory[i] << endl;//cout whats currently in my inventory
cout << "What item weapon would you like to equip?";
/***********Here is the problem*******************/
//how can I make the user choose the actual and correct item in the inventory?
//cin >> equipChoice;
//code goes here
}
else cout << "Error! You currently do not own any items\n";
break;
}
添加到我的Meito Ichimonji
向量中,然后添加另一个向量。如何让用户能够选择正确的项目?我正在寻找这样的东西:
WeaponInventory
对不起,如果我没有说清楚,但我尽力了!
答案 0 :(得分:2)
这不是编码/代码设计的问题,而是概念设计问题。我建议从一个空白的板岩开始并将设计正式化,可能使用关系&#39;即使您可能不在实现中使用RDMS(或者像UML这样的其他形式主义),只是为了在地面上放置一些明确的利害关系。至少这种模型可以清楚地看到各种实体之间的关系,并有助于理解从模型中访问/检索信息所需的内容。很可能代码将是你不必担心的最少的代码。