0

我正在编写单元测试并比较两个对象以确保它们相同但出现错误no operator "==" matches these operands -- operand types are: Item == Item

这是我的代码,有问题的行注释:

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并且container是在别处定义的类。

根据要求,这是Item该类的定义:

class Item {
    public:  
        Item();
};

就是这样——在我的队友编写正确的代码之前,它是一个占位符类。

容器背后的代码目前也很有限,但我认为相关部分如下:

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();
};

我还没有定义方法,Containers因为我首先编写测试代码。

4

1 回答 1

2

==不能用于简单地比较两个不同的对象,除非专门为类定义了比较属性。

于 2021-02-15T18:49:09.050 回答