TransWikia.com

Why is "==" not acceptable here?

Stack Overflow Asked by askman on February 18, 2021

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.

One Answer

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

Answered by askman on February 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP