Danial Wayne
Danial Wayne

Reputation: 348

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.

Upvotes: 2

Views: 3231

Answers (2)

Mark Garcia
Mark Garcia

Reputation: 17708

operator+= is a binary operator and thus requires two operands (e.g. 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 chars. And please don't forget to delete[] all those you new[].

Upvotes: 2

hsun324
hsun324

Reputation: 549

The reason why the += operator works with c-strings is that std::strings 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.

Upvotes: 0

Related Questions