一例奇怪的编译错误(VC6)
VC6 编译下段代码会报错.
将出错行
if (mmm.end() == itr) // ERROR
改写为
if (itr == mmm.end()) // OK
就可通过.
(g++可以通过.)
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)/**//*
error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'class std::_Treeint,struct std::pairint const ,int,struct std::mapint,int,struct std::lessint,class std:
:allocatorint ::_Kfn,struct std::lessint,class std::allocatorint ::const_iterator' (or there is no acceptable conversion)
*/
#pragma warning(disable:4786)
#include map
typedef std::mapint, 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也可以通过.