C++ 无操作员过载时的代码错误

C++ 无操作员过载时的代码错误,c++,operator-overloading,operator-keyword,equals-operator,C++,Operator Overloading,Operator Keyword,Equals Operator,我有一个类使用std::vector>来指示项目及其计数(可以有多个inventoryitems包含相同的项目) 然后,我继续重载clsInventoryItem及其==运算符。 设置结果是: clsInventory.cpp->包括:clsInventory.h clsInventory.h->包括:clsInventoryItem.h clsInventoryItem.h->include:stdafx.h(它依次包括项目的其余部分,不包括这两个头文件) clsInventoryItem的头

我有一个类使用std::vector>来指示项目及其计数(可以有多个inventoryitems包含相同的项目)

然后,我继续重载clsInventoryItem及其==运算符。 设置结果是: clsInventory.cpp->包括:clsInventory.h clsInventory.h->包括:clsInventoryItem.h clsInventoryItem.h->include:stdafx.h(它依次包括项目的其余部分,不包括这两个头文件)

clsInventoryItem的头文件中包含以下内容:

class clsInventoryItem
{
public:
    clsInventoryItem( clsItem* Item, char Quality );
    clsItem* GetItem( );
    char GetQuality( );

    inline bool operator==( const clsInventoryItem& other )
    { /* do actual comparison */
        if (m_Item == other.m_Item
             && m_Quality == other.m_Quality)
        {
            return true;
        }
        return false;
    }
private:
    clsItem* m_Item;
    char m_Quality;
};
它仍然会给出一个错误,即equals函数没有重载(“严重性代码描述项目文件行抑制状态”) 错误C2678二进制“==”:未找到接受类型为“const BrawlerEngineLib::clsInventoryItem”的左侧操作数的运算符(或没有可接受的转换)BrawlerEngineLib d:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\utility 290 ")...
任何人都知道为什么会出现这种情况,以及如何潜在地解决它?

您的
内联布尔运算符==(const clsInventoryItem&other)
应该是常量。
要解决这个问题,您需要将
内联bool操作符==(const clsInventoryItem&other)
更改为
内联bool操作符==(const clsInventoryItem&other)const


此外,您可以去掉关键字
inline
,现代编译器将忽略该关键字,而较旧的编译器仅将其用作提示,并自行决定是否内联该函数。他们做得很好;-)

它仍然会给出一个错误,即equals函数没有重载。请编辑您的问题并添加确切错误消息的文本。如果这是Visual Studio,请从“输出”选项卡复制错误。不应从标题中包含
stdafx.h
。stdafx.h的include必须是源文件的第一个非注释行。编译器将忽略
#上面的所有行包括“stdafx.h”
if(m_Item==other.m_Item
——您正在比较指针值。当然,不应使用
m_Item
的指针值来查看两个
clsItem
对象是否相等。
bool运算符==()
功能应该是常量。如果你还没有,问问自己,“谁负责管理
m_项目”
?“@Joey Vangangelen没问题,很高兴它有帮助!:-)