error C2679: binary '<<' : no operator defined which takes a right-hand operand,该怎么解决
error C2679: binary '<<' : no operator defined which takes a right-hand operand
#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。
------解决方案--------------------
你要用friend函数为CMyString重载<<操作符才行。
------解决方案--------------------
另外VC6对STL支持不够好,也需要当心。
------解决方案--------------------
cout<< 并不认识你的CMyString 类型,要重载的。
------解决方案--------------------
楼主要用"<<"输出类,就得重载"<<"操作符!
#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。
------解决方案--------------------
你要用friend函数为CMyString重载<<操作符才行。
------解决方案--------------------
另外VC6对STL支持不够好,也需要当心。
------解决方案--------------------
cout<< 并不认识你的CMyString 类型,要重载的。
------解决方案--------------------
楼主要用"<<"输出类,就得重载"<<"操作符!