我已經定義了這樣的==操作符重載考慮非靜態成員函數
using namespace std;
class foo {
public:
typedef std::pair< int, int > index;
bool operator == (const index &l, const index &r)
{
return (l.first == r.first && l.second == r.second);
}
void bar()
{
index i1;
i1.first = 10;
i1.second = 20;
index i2;
i2.first = 10;
i2.second = 200;
if (i1 == i2)
cout << "equal\n";
}
};
類不過,我得到的窗口
error C2804: binary 'operator ==' has too many parameters
這個錯誤,這個錯誤在linux
operator==(const std::pair<int, int>&, const std::pair<int, int>&)’ must take exactly one argument
我發現這個話題overloading operator== complaining of 'must take exactly one argument'和似乎是靜態和非靜態功能的問題 一類。但是我不知道如何申請this
例如,這是不正確的
bool operator == (const index &r)
{
return (this->first == r.first && this->second == r.second);
}
我該如何解決呢?
已經std :: pair沒有得到一個==操作符? –