std:映射继承与MFC问题 [英] std:map inheritance with MFC problem

查看:47
本文介绍了std:映射继承与MFC问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有受保护成员的基类:

map< mstring,mstring>

mstring是来自std :: string的派生类,其中包括一些

处理MFC的功能。


它在基类中工作正常,但是当我尝试在派生中使用它时

class,它给我一个错误信息。


C:\Program Files \ Microsoft Visual Studio .NET

2003 \Vc7 \ include \ functional(139):错误C2678:二进制''<'':没有运算符

找到了一个带有''const mstring''类型的左手操作数(或者有

是不可接受的转换)


在我班上有一个运算符<超载。我拿出来看看它可能是问题,但我得到了相同的消息。然后我尝试了一个

测试图< string,string>我得到了同样的信息。我试着用地图<搞乱

mstring,mstring> (添加空格,因为有时

与容器模板VC ++需要一定的空格)。什么都没有。


有趣的是,如果我使用mfc CString类,它可以使用
,但我不想重做额外的功能源自

CString。


任何帮助?


谢谢,

Jay

I have a base class which has a protected member:
map<mstring,mstring>
mstring is a derived class from std::string, which includes some
functionality to cope with MFC.

It works fine in the base class, but when I try to use it in a derived
class, it gives me an error message.

C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\functional(139): error C2678: binary ''<'' : no operator
found which takes a left-hand operand of type ''const mstring'' (or there
is no acceptable conversion)

In my class there is an operator < overload. I took that out to see it
that could be the problem, but I get the same message. I then tried a
test map<string,string> and I get the same message. I tried messing
with map < mstring, mstring > (adding the spaces, because sometimes
with a template of containers VC++ needs certain spaces). Nothing.

The funny thing is that if I use the mfc CString class instead, it
works, but I don''t want to redo the extra functionality deriving from
CString.

Any help?

Thanks,
Jay

推荐答案

Donkeylung写道:
Donkeylung wrote:
C:\Program Files \ Microsoft Visual Studio .NET
2003 \Vc7 \include \ functional(139):错误C2678:二进制''<'':找不到运算符
,它采用类型''const mstring'的左手操作数'(或者
是不可接受的转换)

在我班上有一个操作员<超载。
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\functional(139): error C2678: binary ''<'' : no operator
found which takes a left-hand operand of type ''const mstring'' (or there
is no acceptable conversion)

In my class there is an operator < overload.




你能否展示两件事:

1)你如何从std :: string派生出来?

2)你如何实现运营商<在mstring?

3)如果std :: string占有,你如何使用map?



Can you please show two things:
1) How you derive from std::string?
2) How do you implement operator < in mstring?
3) How do you use map in case of std::string possession?


1)

typedef class mfc_string:

public string

{

.....

} mstring;

2)

这是2运营商<重载(他们最初没有采取

const参数的方式,我仍然得到错误)

mfc_string mfc_string :: operator<(const mfc_string& p)

{

double left = atof(this-> c_str());

double right = atof(p.c_str());

char enter_string [50];

sprintf(enter_string,"%f",left-right);

return mstring(enter_string);

}


mfc_string mfc_string :: operator<(const double& right)

{

double left = atof(this-> c_str());

char enter_string [50];

sprintf(enter_string,"%f",left-right);

返回mstring(enter_string);

}


3)当我使用map< string,string>时,它仅用于测试。


另外请注意,只有当我使用[]表示法时才会出现错误。如果它是访问先前定义的密钥(在基础

类中,它没关系)或者是否正在创建新密钥,那么
无关紧要派生

等级。


谢谢,

jay

1)
typedef class mfc_string :
public string
{
.....
}mstring;
2)
Here are the 2 operator < overloads (they originally did not take
const arguments either way, I still get the error)
mfc_string mfc_string::operator<(const mfc_string& p)
{
double left = atof(this->c_str());
double right = atof(p.c_str());
char enter_string[50];
sprintf(enter_string, "%f", left-right);
return mstring(enter_string);
}

mfc_string mfc_string::operator<(const double& right)
{
double left = atof(this->c_str());
char enter_string[50];
sprintf(enter_string, "%f", left-right);
return mstring(enter_string);
}

3) When I used map<string, string>, it was only for a test.

Another note, the error only occurs when I use the [] notation. It
doesn''t matter if it is accessing a previously defined key(in the base
class, where it is fine) or if is creating a new key in the derived
class.

thanks,
jay


Donkeylungéérit:
Donkeylung a écrit :
mstring是来自std :: string的派生类
mstring is a derived class from std::string



std :: string并不意味着要派生,它没有任何虚拟功能。

因此,如果你尝试这样做,你可能会遇到问题。



std::string isn''t meant to be derived, it doesn''t have any virtual function.
Thus you will probably have issues if you try doing that.


这篇关于std:映射继承与MFC问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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