Overloading custom string operator += in c++

I am working to recreate the various c++ types in order to better understand how they work. I am currently stuck on the += operator and can not find what the issue is with my declaration. Here is the code for my Class:

class String {
    int size;
    char * buffer;
public:
    String();
    String(const String &);
    String(const char *);
    int length(){return size;};

    friend bool operator==(const String &, const String &);
    friend bool operator<=(const String &, const String &);
    friend bool operator<(const String &, const String &);
    friend ostream & operator<<(ostream &, const String &);

    char operator[](const int);
//  friend String operator+=(const String &,const char * p);
    friend String operator+=(const char * p);

};

I am getting these to work as planned with the exception of the += operator defined as:

String operator+=(const char * p){
int p_size = std::char_traits<char>::length(p);
int new_size = size+p_size;
char * temp_buffer;
temp_buffer = new char(new_size);

for(int i=0; i<size; i++){
    temp_buffer[i] = buffer[i];
}

for(int i=size, j=0; j<p_size;i++,j++){
    temp_buffer[i] = p[j];
}

delete buffer;
buffer = new char[new_size];
size = new_size;
for(int i=0; i<size; i++){
    buffer[i] = temp_buffer[i];
}
return *this;
}

My errors are string.h:29: error: âString operator+=(const char*)â must have an argument of class or enumerated type string.cpp:28: error: âString operator+=(const char*)â must have an argument of class or enumerated type

Any info of what I am doing wrong during overloading is appreciated.


operator+= is a binary operator and thus requires two operands (eg myString += " str", , where myString and " str" are the operands).

However, you have an ill-formed operator+= , as it only accepts a single argument. Note that your operator+= is a stand-alone function (not a class method), with it returning a String and accepting a single const char* argument.

To solve your problem, make your operator+= a member function/method, because by then, you'll have an implicit this parameter, which will be used as the left hand side operand.

class String {
    ...
    String& operator+=(const char * p);
};

and its definition

String& String::operator+=(const char * p) {
   ...
   return *this;
}

Notice that you are now returning a reference to *this , and its return type changed to String& . These are in accordance to guidelines in Operator overloading .

CRITICAL UPDATE:

temp_buffer = new char(new_size);

Noooo! You are allocating a single char and initializing it to new_size , and that is not what you desire. Change it to brackets.

temp_buffer = new char[new_size];

Now, you are properly allocating an array of new_size number of char s. And please don't forget to delete[] all those you new[] .


The reason why the += operator works with c-strings is that std::string s have an implicit conversion constructor from c-strings.

Since you already have a conversion constructor, you should just make a += operator that takes a String .

链接地址: http://www.djcxy.com/p/73108.html

上一篇: 使用朋友函数在c ++中重载I / O操作符

下一篇: 在c ++中重载自定义字符串运算符+ =