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。