关于小于号的重载问题

hmsuccess 2008-10-29 02:01:13
怎么去重载"< ,>"两个符号,一下是我的类结构,谢谢
class Dstring
{
public:
Dstring(){}
Dstring(const char* str);
Dstring(string str);
Dstring(const Dstring& str);
Dstring& operator=(const char* str);
Dstring& operator=(const Dstring& str);
bool operator==(const Dstring& );
bool operator>(Dstring& str);
bool operator!=(const Dstring& );
bool operator<(Dstring& str);
Dstring& operator+=(const string& str);
Dstring& operator+(const string& str);
Dstring& operator+(const char* str);
string getString()const {return srcstr;}
private:
string srcstr;

};

错误信息:error C2679: binary '<' : no operator found which takes a right-hand operand of type 'const Dstring'
...全文
424 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmsuccess 2008-10-29
  • 打赏
  • 举报
回复
上面的问题已解决,,吸血各位
hmsuccess 2008-10-29
  • 打赏
  • 举报
回复
按照自己的要求,进行比较大小
mmidd 2008-10-29
  • 打赏
  • 举报
回复
string 类型已经重载了 < 号,你拿来用就行吧
chenzhp 2008-10-29
  • 打赏
  • 举报
回复
你没有发现那是比较类里面的成员变量stcstr的么?你要想再外面直接使用类的对象来达到这种目的,就需要重载。
重载到内部数据类型为止
elegant87 2008-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lann64 的回复:]
C/C++ codebool operator < (const Dstring& str)
{
return srcstr<str.srcstr;
}
[/Quote]
正解!我弄不明的是string类本身就可以比较大小的!为什么还要重载呢?
lann64 2008-10-29
  • 打赏
  • 举报
回复
bool operator < (const Dstring& str)
{
return srcstr<str.srcstr;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧