error C2679: binary '<<' : no operator defined which takes a right-hand operand

风泥 2012-07-27 11:03:35
#include <iostream>
#include <cstring>
using namespace std;

class CMyString
{
private:
char *str;
public:
CMyString(char *s = NULL)
{
if(s == NULL)
{
str = new char[20];
str = '\0';
}
else
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
}
~CMyString()
{
if(str != NULL)
{
delete str;
str = NULL;
}
}

public:
int GetLength() const
{
return strlen(str);
}

bool IsEmpty() const
{
if(strlen == 0)
return true;
}

void Empty()
{
str = '\0';
}

char GetAt(int nIndex) const
{
if(nIndex < 0 || nIndex > (strlen(str) - 1))
{
cout << "下标越界" << endl;
}
else
return str[nIndex];
}

char operator[](int nIndex) const
{
if(nIndex < 0 || nIndex > (strlen(str) - 1))
{
cout << "下标越界" << endl;
}
else
return str[nIndex];
}

void SetAt(int nIndex, char ch)
{
if(nIndex < 0 || nIndex > (strlen(str) - 1))
{
cout << "下标越界" << endl;
}
else
{
str[nIndex] = ch;
}
}

// operator char*() const
// {
// return str;
// }

const CMyString& operator=(const CMyString& stringSrc)
{
delete[] str;
str = new char[strlen(stringSrc.str) + 1];
strcpy(str, stringSrc.str);
return (*this);
}

};

int main()
{
//char *s = "hello";
CMyString s1 = "hello";
CMyString s2 = "world";
s1 = s2;
cout << s1 << endl;//问题就在这个地方
return 0;
}
//会报这样的错误error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CMyString' (or there is no acceptable conversion) 应该怎么避免了,我用的是VC6.0。
...全文
499 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
风泥 2012-08-13
  • 打赏
  • 举报
回复
嗯 多谢各位大神,最近基本已经弄清楚了
zfk198687 2012-07-28
  • 打赏
  • 举报
回复
楼主要用"<<"输出类,就得重载"<<"操作符!
allenbein 2012-07-27
  • 打赏
  • 举报
回复
cout<< 并不认识你的CMyString 类型,要重载的。
pathuang68 2012-07-27
  • 打赏
  • 举报
回复
另外VC6对STL支持不够好,也需要当心。
pathuang68 2012-07-27
  • 打赏
  • 举报
回复
你要用friend函数为CMyString重载<<操作符才行。

65,187

社区成员

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

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