I have a base class which has a protected member:map<mstring,mstring>mstring is a derived class from std::string, which includes somefunctionality to cope with MFC. It works fine in the base class, but when I try to use it in a derivedclass, it gives me an error message. C:\Program Files\Microsoft Visual Studio .NET2003\Vc7\include\functional(139): error C2678: binary ''<'' : no operatorfound which takes a left-hand operand of type ''const mstring'' (or thereis no acceptable conversion) In my class there is an operator < overload. I took that out to see itthat could be the problem, but I get the same message. I then tried atest map<string,string> and I get the same message. I tried messingwith map < mstring, mstring > (adding the spaces, because sometimeswith a template of containers VC++ needs certain spaces). Nothing. The funny thing is that if I use the mfc CString class instead, itworks, but I don''t want to redo the extra functionality deriving fromCString. 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 takeconst 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. Itdoesn''t matter if it is accessing a previously defined key(in the baseclass, where it is fine) or if is creating a new key in the derivedclass. 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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 00:13