stl map中的关键比较问题 [英] key compare problem in stl map

查看:77
本文介绍了stl map中的关键比较问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在stl地图中遇到密钥比较问题。下面是我的

代码的一部分..任何人都可以告诉我这里可能有什么问题...


我使用VC ++ 6.0
< br $>
代码:

=====

等级MyKey

{


public:

unsigned long value;

unsigned long type

bool operator<(const MyKey& right)

{

return(类型< right.type)&&

(值< right.value);

}

};


class MapTest

{

public:

std :: map< MyKey,MyClass * myMap;


MapTest();

~MapTest();

void addElement(MyClass * pClass);

OmciCommonMe * findElement(MyKey key);

};



错误:

-------

c:\ program program \ microsoft visual studio \vc98 \include \ functional(86):

错误C2678:二进制''<'':没有运算符定义的左手

类型''const class MyKey''的操作数(或者没有可接受的

转换)

c:\program files \ microsoft visual

studio\vc98\include\functional(86):编译类模板时

成员函数''bool __thiscall std :: less< class MyKey>: :运算符

()(const类MyKey&,const类MyKey&)const''

解决方案

" NewToCPP" <他**** @ yahoo.com写了留言

新闻:11 ********************** @ 73g2000cwn.googlegro ups.com ...


>我在stl地图中遇到密钥比较问题。下面是我的

代码的一部分..任何人都可以告诉我这里可能有什么问题...


我使用VC ++ 6.0
< br $>
代码:

=====

等级MyKey

{


public:

unsigned long value;

unsigned long type

bool operator<(const MyKey& right)

{

return(类型< right.type)&&

(值< right.value);

}

};


class MapTest

{

public:

std :: map< MyKey,MyClass * myMap;


MapTest();

~MapTest();

void addElement(MyClass * pClass);



addElement的代码在哪里?


OmciCommonMe * findElement(MyKey key);



findElement的代码在哪里?


};


错误:

-------

c:\program files \ microsoft visual studio\vc98\include\functional(86):

错误C2678:二进制''<'':没有操作员定义哪个需要左手

类型''const class MyKey''的操作数(或者没有可接受的

转换)

c:\program files \ microsoft visual
studio\vc98\include\functional(86):编译类模板时

成员函数''bool __thiscall std :: less< class MyKey> :: operator < br $>
()(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
< br $>
代码:

=====

等级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 my
code .. could anyone tell me what might be wrong here...

I am using VC++ 6.0

code:
=====
class MyKey
{

public:
unsigned long value;
unsigned long type
bool 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-hand
operand of type ''const class MyKey'' (or there is no acceptable
conversion)
c:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<class MyKey>::operator
()(const class MyKey &,const class MyKey &) const''

解决方案

"NewToCPP" <he****@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...

>I am having problem with key compare in stl map. Below is part of my
code .. could anyone tell me what might be wrong here...

I am using VC++ 6.0

code:
=====
class MyKey
{

public:
unsigned long value;
unsigned long type
bool 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-hand
operand of type ''const class MyKey'' (or there is no acceptable
conversion)
c:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member 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 my
code .. 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 a
member 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 my
code .. could anyone tell me what might be wrong here...

I am using VC++ 6.0

code:
=====
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中的关键比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆