Been working with operator overloading for the first time and am running into quite the standstill with this one. I finally got rid of the code I had written and decided to start from scratch. If anyone has any advice on how to code this for me it be a great help for me to see an example on how this would work.

This is what I need to do for it.

MyString MyString::operator+(const MyString& rightOp) const

The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.


Here is what I originally had if this helps anyone solve my issue.

MyString MyString::operator+(const MyString& rightOp) const
	{
	strcat(this->stringStorage, rightOp.stringStorage);
	return *this;
	}

Recommended Answers

All 10 Replies

I see two things wrong:
1) remove the const keyword at the end -- the function is not const because it changes the value of stringStorage.

2)You need another class object. What you wrote was for the += operator, not the + operator.

MyString MyString::operator+(const MyString& rightOp)
{
    MyString tmp = *this; // requires overloaded = operator
    strcpy(tmp.stringStorage, rightOp.stringStorage);
    return tmp;
}

I don't have a choice on the const keyword on the end because its part of the assignment. Should I be returning rightOp then?

The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.

Don't you want this function to be outside of the class?

class MyString
{
    // ...
};

MyString operator+ (const MyString &lhs, const MyString &rhs)
{
    return MyString(lhs) + MyString(rhs);
}

Or something like that?

I went ahead and applied the changes but I left const on the end because of the requirement would that throw things off?

MyString MyString::operator+(const MyString& rightOp) const
        {
        MyString tmp = *this;
        strcpy(tmp.stringStorage, rightOp.stringStorage);
        return tmp;
        }

Yup -- I tested that too and it compiled ok.

Don't you want this function to be outside of the class?

class MyString
{
    // ...
};

MyString operator+ (const MyString &lhs, const MyString &rhs)
{
    return MyString(lhs) + MyString(rhs);
}

Or something like that?

No -- that is illegal syntax for non-class methods.

error C2270: '+' : modifiers not allowed on nonmember functions

Alright well have another question about a different + overloading because I have to do three and they are giving issues.

MyString MyString::operator+(const char* rightOp) const
        {
        strcat(this->stringStorage, rightOp);
        return *this;
        }

#

MyString MyString::operator+(const char* rightOp) const

The addition operator should be overloaded to take a MyString object and a C-style string, concatenate their text together, and return a new MyString object that contains the result.

Anymore advice would be great. I have to write three + overloads and they are just giving me an issue where the other ones I did not have as much problem.

Look at the function I posted above. All you have to do is change the parameter types in the three overloads

MyString MyString::operator+(const MyString& rightOp) const
MyString MyString::operator+(const char* rightOp) const
MyString MyString::operator+(const std::string& rightOp) const

char *strcat( char *str1, const char *str2 );

That's the prototype for strcat() from the online reference I usually refer to. I always thought str1 needed to be a null terminated char array and str2 needed to be either a null terminated char array or a string literal. I am also almost certain that the return value of char* will be a null terminated char array. From your previous post I thought that one of the requirements was that the MyString member varialbe called stringStorage could not be null terminated. If that restriction still holds, then you will have to be careful regarding using strcat() and other C style string handling functions to meet the assigment requirements.

I was able to finish the assignment today he showed me how the plus overload was suppose to work which made the other two alot easier to code, thank you very much for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.