I’m attempting to create an inventory system using a vector implementation, but I seem to be having some troubles. I’m running into issues using a struct I made. NOTE: This isn’t actually in a game code, this is a separate Solution I am using to test my knowledge of vectors and structs!
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;
}
The preceeding code gives me the following error:
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)
There is more to the error, if you need it please let me know. I bet it’s something small and silly, but I’ve been thumping at it for over two hours. Thanks in advance!
The
findmethod does not know how to compare twoaItemobjects for equality. You need to define the==operator in your struct definition, like this:This will allow
findto determine if twoaItemobjects are equal, which is necessary for the algorithm to work.