簡體   English   中英

嘗試插入 std::set 時“不匹配 'operator<'”

[英]“no match for 'operator<'” when trying to insert to a std::set

我正在使用 gcc 4.3.3 嘗試編譯以下代碼:

struct testStruct {  
int x;  
int y;  
bool operator<(testStruct &other) { return x < other.x; }  
testStruct(int x_, int y_) {  
    x = x_;  
    y = y_;  
}  
};  


int main() {
multiset<testStruct> setti;  
setti.insert(testStruct(10,10));  
return 0;  
}

我收到此錯誤:
/usr/include/c++/4.4/bits/stl_function.h|230|錯誤:“__x < __y”中的“operator<”不匹配
我懷疑我沒有按照應該做的那樣進行運算符重載,但我無法查明確切的問題。 我在這里做錯了什么?

運算符必須是 const 並采用 const 引用:

bool operator<(const testStruct &other) const { return x < other.x; }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM