Expression : invalid operator 解决方法
確實如下文所說。
VS2008的sort()函數(shù)的用法貌似挺郁悶的。。。
?
前些時候?qū)懥藗€sort的compare函數(shù),錯誤"Expression : invalid operator <",baidu+google了一下,沒有找到比較明確的說法,不過找到了微軟的一個網(wǎng)頁,說得算是很清楚,不過看得不太明白。意思大概是出這個錯是因為VS2005,VS2008后的sort()里,用的是所謂的“?strict weak ordering”,也就是說,如果a==b,則返回的應(yīng)該是false,如果返回的是true,則會出上面的錯。
?
網(wǎng)頁:http://support.microsoft.com/kb/949171
?
以下摘抄網(wǎng)頁中的說法:
?
1.strict weak ordering
?
舉例說明如下:?
· Strict: pred(X, X) is always false.
· Weak: If !pred(X, Y) && !pred(Y, X), X==Y.
· Ordering: If pred(X, Y) && pred(Y, Z), then pred(X, Z).
?
?
2.出現(xiàn)"Expression : invalid operator <"的寫法
?
bool CustPredicate (int elem1, int elem2 )
{
????if(elem1 > elem2)
????????return true;?
????if (elem1 < elem2)
????????return false;
????return true;
}
?
?
3.為了解決錯誤,應(yīng)把以上代碼改寫為以下兩種中的任一種:
?
(1)
?
bool CustPredicate (int elem1, int elem2 )
{
????if(elem1 > elem2)
????????return true;?
????if (elem1 < elem2)
????????return false;
????return false; //Should return false if both the vaules are same
}
(2)
Second Option:-
bool CustPredicate (int elem1, int elem2 )
{
????return elem1 > elem2;
}
總結(jié)
以上是生活随笔為你收集整理的Expression : invalid operator 解决方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。