一例奇怪的編譯錯誤(VC6)
VC6 編譯下段代碼會報錯.
將出錯行
if (mmm.end() == itr) // ERROR
改寫為
if (itr == mmm.end()) // OK
就可通過.
(g++可以通過.)
/**//*
error C2679: binary '==' : no operator defined which takes a right-hand Operand of type 'class std::_Tree<int,struct std::pair<int const ,int>,struct std::map<int,int,struct std::less<int>,class std:
:allocator<int> >::_Kfn,struct std::less<int>,class std::allocator<int> >::const_iterator' (or there is no acceptable conversion)
*/
#pragma warning(disable:4786)
#include <map>
typedef std::map<int, int> MYMAP;
int main(int argc, char* argv[])
...{
MYMAP mmm;
MYMAP::const_iterator itr = mmm.find(1234);
// if (mmm.end() == itr) ERROR!!! (VC6)
// if (itr == mmm.end()) OK
if (mmm.end() == itr)
return 123;
return 0;
}
粗略地分析是map.end()返回的是iterator類型,
而iterator::Operator==(const_iterator)沒有定義.
將map.end()強制為const_iterator也可以通過.