设置自定义字符串类问题

Set with Custom String Class Problem

本文关键字:问题 字符串 自定义 设置      更新时间:2023-10-16

我写了一个自定义字符串类。我想用STL集合它。我已经重载了操作符<但它仍然给我的问题>

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const     String' (or there is no acceptable conversion)
1>    could be 'String &String::operator =(const String &)'
1>           'String &String::operator =(const char *)'
1>          'String &String::operator =(const wchar_t *)'
1>          while trying to match the argument list '(const String, const String)'

我猜,它要求重载操作符= (const String, const String)

但是创建这样一个重载函数是不可能的

我的String类是this

String ();
String (const char * pStr);
String (const long int pData);
String (const double  pData);
String (const int pData);
String (const wchar_t * pStr);
//Copy Constructors
String (const String& rhs);
String (const String& rhs, const int pStartIndex, const int pNumChar);
//Overloaded Operators
String & operator= (const String & rhs);
String & operator= (const char * rhs);
String & operator= (const wchar_t * rhs);
String   operator+ (const String& rhs);
//String &  operator+= (const char ch);
String & operator+= (const String& rhs);
friend bool operator== (const String& lhs, const String& rhs);
friend bool operator< (const String& lhs, const String& rhs) {
    return strcmp(lhs.vStr, rhs.vStr);
}
friend ostream& operator<< (ostream& ostr, String& rhs);
char & operator[] (int pIndex);
char   operator[] (int pIndex) const;
const char * String::Buffer () const;
wchar_t * GetTChar();
int String::GetLength () const;
~String ();

"未找到手操作数类型为'const String'"

似乎你有一个表达式像

a=b;

,其中ab都是const String

不能给const赋值(尽管编译器会拼命寻找这样的赋值实现)

好吧,我只能用你提供的信息来回答你提出的问题,答案是这对我来说是可行的