AnswerBun.com

Why is "==" not acceptable here?

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.

Stack Overflow Asked by askman on February 18, 2021

1 Answers

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!

Related Questions

Get all tables where there is no booking on this time or date

2  Asked on November 24, 2021 by tierno

 

How to remove first view pager item in kotlin?

1  Asked on November 24, 2021 by marlen-schreiner

     

Pandas update values using loc with repeated indices

2  Asked on November 24, 2021 by tyler-klein

     

ORDER BY with priority

2  Asked on November 22, 2021 by ush_63100

 

Get frequency of tokens by group in pandas

1  Asked on November 22, 2021 by ravanelli

       

Repeat vector elements according to values in matrix

2  Asked on November 22, 2021 by maksim-tarasenko

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.