在我的代码中,我遇到了37个相同类型c2678的错误;二进制'operator':未定义任何采用'type'类型的左操作数的运算符(或没有可接受的转换)

我试图通过重载==运算符(包括STL“实用程序”)来消除错误。
http://msdn.microsoft.com/en-us/library/86s69hwc(v=vs.80).aspx
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading

但这仍然行不通。任何帮助表示赞赏。

最佳答案

该标头为某些标准类型提供operator==的重载,但对于您自己的类型,它不会神奇地对其进行重载。如果要使类型具有相等性,则必须自己重载运算符,例如:

bool operator==(my_type const & a, my_type const & b) {
    return a.something == b.something
        && a.something_else == b.something_else;
}

// You'll probably want this as well
bool operator!=(my_type const & a, my_type const & b) {
    return !(a == b);
}

07-25 22:35
查看更多