2

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!

1
  • 1
    if you use vector<aItem*> and push_back(&healingPotion) your problem will go away. this will also allow you to subclass aItem.
    – Ray Tayek
    Nov 9, 2012 at 23:24

3 Answers 3

5

find looks for something that's equal to the item in the vector. You say you want to search using strings, but you haven't written code for that; it's trying to compare the entire struct. And you haven't written code to compare entire structs, so it's giving you an error.

The simplest solution is to use an explicit loop instead of find.

If you want to find things by string, use the find_if variant and write a predicate function that looks at the string. Or if you want to find things by the entire struct you can define an operator == on the struct that compares both itemName and damage.

Or you might also consider using the map or unordered_map data structures instead of vector. The map containers are designed for fast lookup using a key (such as the string).

2
  • Sorry, my "string" comment was merely designed to let people know I had successfully searched while looking for strings, but once I switched to struct items, it stopped working. Sorry I wasn't clear! Nov 9, 2012 at 18:52
  • +1 for 'The simplest solution is to use an explicit loop instead of find.' It's not worth jumping through the hoops to try and get find working with anything but direct comparisons.
    – Kylotan
    Nov 9, 2012 at 19:31
3

The find method does not know how to compare two aItem objects for equality. You need to define the == operator in your struct definition, like this:

bool operator==(aItem other)
{
    if (itemName == other.itemName && damage == other.damage)
        return true;
    else
        return false;
}

This will allow find to determine if two aItem objects are equal, which is necessary for the algorithm to work.

5
  • 4
    Whenever you write if (c) return true; else return false;, just write return c;.
    – GManNickG
    Nov 9, 2012 at 22:18
  • Of course. Just looking to maximize clarity. Would you suggest always providing the best code in answers, or do you think it's sometimes worthwhile to keep things simpler to ensure the person asking the question understands the answer, especially the part relevant to the question?
    – Jeff
    Nov 13, 2012 at 19:14
  • Maximizing clarity means omitting noise. So yeah, maximal clarity is good, unnecessary branches is bad.
    – GManNickG
    Nov 14, 2012 at 17:02
  • 1
    I can't disagree with that, but I would think that to a beginner, the explicit (albeit superfluous) branches are easier to understand, which allows them to focus on the core of the topic - in this case, operator overloading.
    – Jeff
    Nov 14, 2012 at 18:33
  • For my $.02, I don't mind the else return false, as I may not have known that you don't have to do that- newbies like me seem to get dumber and dumber, remember, as you get smarter and smarter. :) Nov 20, 2012 at 17:27
0

try something like:

#include <iostream>
#include <vector>
using namespace std;
struct item {
    item(string const name,int const damage):name_(name),damage_(damage) {

    }
    string name_;
    int damage_;
};
int main(int argc, char** argv) {
    vector<item *> items;
    item healingPostion("cure light",-10);
    item fireballPostion("fireball",10);
    items.push_back(&healingPostion);
    items.push_back(&fireballPostion);
    if(find(items.begin(), items.end(), &fireballPostion) != items.end()) {
        cout << "Found";
    }
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.