我正在嘗試使用矢量實現來創建庫存系統,但我似乎遇到了一些麻煩。我遇到了使用我製作的結構的問題。注意:這實際上並不在遊戲代碼中,這是一個單獨的解決方案,我正在使用它來測試我對向量和結構的知識!如何搜索向量中的結構項目?
struct aItem
{
string itemName;
int damage;
};
int main()
{
aItem healingPotion;
healingPotion.itemName = "Healing Potion";
healingPotion.damage= 6;
aItem fireballPotion;
fireballPotion.itemName = "Potion of Fiery Balls";
fireballPotion.damage = -2;
vector<aItem> inventory;
inventory.push_back(healingPotion);
inventory.push_back(healingPotion);
inventory.push_back(healingPotion);
inventory.push_back(fireballPotion);
if(find(inventory.begin(), inventory.end(), fireballPotion) != inventory.end())
{
cout << "Found";
}
system("PAUSE");
return 0;
}
見前代碼給我下面的錯誤:
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3186): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'aItem' (or there is no acceptable conversion)
還有更多的錯誤,如果你需要它,請讓我知道。我敢打賭,這是一個小而愚蠢的事情,但我一直在嘔吐超過兩個小時。提前致謝!
如果使用矢量和的push_back(healingPotion)你的問題就會迎刃而解。這也將允許你繼承一個項目。 –