0

I'm writing a unit test and comparing two objects to ensure they're the same but am getting the error no operator "==" matches these operands -- operand types are: Item == Item

This is my code with the line in question commented:

SCENARIO("A container can be created, and items added and removed from it.", "[container]") {

    GIVEN("A container and some items.") {
        
        Container merchant("Merchant", "Merchant Inventory");
        Item sword;

        WHEN("An item is added to an empty container.") {
            merchant.addItem(sword);

            THEN("The item is in position 0 in the container.") {
                Item firstItem = merchant.get_contents().front();
                REQUIRE(firstItem == sword);    // I'm trying to assert that the item in the vector that
            }                                   // is returned is the item that was entered.
        }
    }
}

Item and container are classes which are defined elsewhere.

As requested, here is the definition of the Item class:

class Item {
    public:  
        Item();
};

That's literally it - it's a placeholder class until my team mate writes the proper code.

The code behind containers is limited currently also, but here are what I think are the relevant parts:

class Container {

    private:
        std::vector<Item> contents;  //  Capcity limited to 10 to begin with
    
    public:
        Container(std::string name, std::string description);

        std::vector<Item> get_contents();
};

I've not defined the methods in Containers yet as I'm writing the test code first.

askman
  • 447
  • 4
  • 14
  • 8
    Just a wild guess (not seeing the definition of the `Item` class) but I suppose it's because `operator==` isn't defined for that class? – Adrian Mole Feb 15 '21 at 18:31
  • 3
    Create a [mcve] – eerorika Feb 15 '21 at 18:32
  • 1
    Did you overload `==` for the `Item` class? Would need to see `Item` class implementation to know for sure, but I suspect you need this: https://stackoverflow.com/questions/1691007/whats-the-right-way-to-overload-operator-for-a-class-hierarchy – Gillespie Feb 15 '21 at 18:32
  • 1
    You need to write your own comparison operator for your class. – JHBonarius Feb 15 '21 at 18:32
  • Do you need to overload operators even if you just want them to do the same as the default? All I want here is to check that the two objects are the same. – askman Feb 15 '21 at 18:37
  • 1
    What do you mean "the same"? If you want to know if they are the same object in memory, I suppose you could compare the addresses like `&firstItem == &sword`. However, that's assuming the vector is storing references and not copies as it currently is. But if you want to know if the data inside two copies of an object are the same, you'll have to implement `==` for that class. – Gillespie Feb 15 '21 at 18:42
  • C++20 has a [default comparison](https://en.cppreference.com/w/cpp/language/default_comparisons), which works for simple types. For older standards you'll have to do it yourself – JHBonarius Feb 15 '21 at 18:45
  • 1
    In short: in your particular case, you are storing a *copy* of `sword` in the vector, so you pretty much would need to implement `==` to compare. If instead you stored a pointer to sword (which would be invalid as soon as `sword` goes out of scope, unless you put `sword` on the heap), you could compare the memory addresses without implementing `==`. – Gillespie Feb 15 '21 at 18:47

1 Answers1

2

== cannot be used to simply compare two different objects unless specifically defined for the class to compare attributes.

askman
  • 447
  • 4
  • 14