0

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

我试图通过重载 == 运算符,包括 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

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

4

1 回答 1

1

该标头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);
}
于 2012-07-27T12:52:16.527 回答