studio\vc98\include\functional(86):编译类模板时 成员函数''bool __thiscall std :: less< class MyKey> :: operator ()(const类MyKey&,const类MyKey&)const'' 如果你向我们展示了它们会有所帮助修正错误的代码。 NewToCPP写道: I我在stl地图中遇到密钥比较问题。以下是我的 代码的一部分..谁能告诉我这里可能有什么问题... < snip> bool operator<(const MyKey& right) { return(type< right.type) && (值< right.value); } < snip> 1.你需要_const_,作为一个独立的功能比作为一个 成员函数更好: bool operator< (const MyKey& lhs,const MyKey& rhs); 2.你_must_有严格的弱订购,否则会发生坏事。 祝你好运,JE NewToCPP写道: 我遇到了问题在stl地图中使用键比较。下面是我的 代码的一部分..任何人都可以告诉我这里可能有什么问题... 我使用VC ++ 6.0 代码: ===== 等级MyKey { public: unsigned long value; unsigned long type bool operator<(const MyKey& right) 应该是: bool运算符<(const MyKey& right)const { 返回(类型< right.type)&& (值< right.value); } //更好的是,让它成为一个免费的功能。 朋友布尔运算符<(const MyKey& left,const MyKey& right) { 返回类型< right.type&&价值< right.value; } }; I am having problem with key compare in stl map. Below is part of mycode .. could anyone tell me what might be wrong here... I am using VC++ 6.0 code:=====class MyKey{ public:unsigned long value;unsigned long typebool operator<(const MyKey& right){return ( type< right.type) &&( value < right.value );}}; class MapTest{public:std::map<MyKey, MyClass* myMap; MapTest();~MapTest();void addElement(MyClass* pClass);OmciCommonMe* findElement(MyKey key);}; Error:-------c:\program files\microsoft visual studio\vc98\include\functional(86) :error C2678: binary ''<'' : no operator defined which takes a left-handoperand of type ''const class MyKey'' (or there is no acceptableconversion)c:\program files\microsoft visualstudio\vc98\include\functional(86) : while compiling class-templatemember function ''bool __thiscall std::less<class MyKey>::operator()(const class MyKey &,const class MyKey &) const'' 解决方案 "NewToCPP" <he****@yahoo.comwrote in messagenews:11**********************@73g2000cwn.googlegro ups.com...>I am having problem with key compare in stl map. Below is part of mycode .. could anyone tell me what might be wrong here...I am using VC++ 6.0code:=====class MyKey{public:unsigned long value;unsigned long typebool operator<(const MyKey& right){return ( type< right.type) &&( value < right.value );}};class MapTest{public:std::map<MyKey, MyClass* myMap;MapTest();~MapTest();void addElement(MyClass* pClass);Where is the code for addElement? OmciCommonMe* findElement(MyKey key);Where is the code for findElement? }; Error:-------c:\program files\microsoft visual studio\vc98\include\functional(86) :error C2678: binary ''<'' : no operator defined which takes a left-handoperand of type ''const class MyKey'' (or there is no acceptableconversion) c:\program files\microsoft visualstudio\vc98\include\functional(86) : while compiling class-templatemember function ''bool __thiscall std::less<class MyKey>::operator()(const class MyKey &,const class MyKey &) const''It would help if you showed us the acutal code that was cuasing the error.NewToCPP wrote:I am having problem with key compare in stl map. Below is part of mycode .. could anyone tell me what might be wrong here... <snip>bool operator<(const MyKey& right){return ( type< right.type) &&( value < right.value );}<snip> 1. You need _const_, better as a free-standing function than as amember function:bool operator<(const MyKey& lhs, const MyKey& rhs); 2. You _must_ have "strict weak ordering", or bad things will happen. Best regards, JE NewToCPP wrote:I am having problem with key compare in stl map. Below is part of mycode .. could anyone tell me what might be wrong here...I am using VC++ 6.0code:=====class MyKey{public: unsigned long value; unsigned long type bool operator<(const MyKey& right)that should be:bool operator<( const MyKey& right ) const { return ( type< right.type) && ( value < right.value ); }//Better yet, make it a free function. friend bool operator<( const MyKey& left, const MyKey& right ) {return type < right.type && value < right.value;} }; 这篇关于stl map中的关键比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 02:16