Today is the first day I have attempted Operator Overloading and am running into a few errors so any assistance would be great!

The first one I'm having issues with is here:

const MyString& operator=(const char* rightOp)

The assignment operator should also be overloaded to allow assignment of a C-style string to a MyString object.

Here is my code for it at this point.

const MyString& MyString::operator=(const char* rightOp)
        {
        delete[] stringStorage;
        stringStorage = new char[rightOp.stringSize];
        for(int i  = 0; i < rightOp.stringSize; i++)
                stringStorage[i] = rightOp.stringStorage[i];
        stringSize = rightOp.stringSize;
        return *this;
        }

MyString.cpp: In member function âconst MyString& MyString::operator=(const char*)â:
MyString.cpp:60: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:61: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:62: error: request for member âstringStorageâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:63: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â

The other one I am having issue with is.

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.

MyString MyString::operator+(const MyString& rightOp) const
        {
        strcat(rightOp, rightOp);
        return strcat;
        }

MyString.cpp: In member function âMyString MyString::operator+(const MyString&) constâ:
MyString.cpp:81: error: cannot convert âconst MyStringâ to âchar*â for argument â1â to âchar* strcat(char*, const char*)â
MyString.cpp:82: error: conversion from âchar* (*)(char*, const char*)throw ()â to non-scalar type âMyStringâ requested


I am hoping a lot of these errors are common mistake so we'll see. Any help from you guys would be great. Thanks in advance!

Many problems. You're mixing class vs non-class string handling.

Use what I'm providing to rework your functions.

const MyString& MyString::operator=(const char* rightOp) 
{ 
   int i = 0;

   if (NULL != stringStorage)
   {
      delete[] stringStorage; 
   }

   stringSize = strlen( rightOp );

   stringStorage = new char[ stringSize + 1 ];
    for( i  = 0; i < stringSize; i++)
       stringStorage[i] = rightOp[i];

    stringStorage[i] = 0;
    return *this;
}

Alright I think I see a lot of the problems I was having on that overload. My problem on the second one mostly is strcat how do I combine the two strings in that one?

the problem i see is that your treating the char as it was a MyString. for chars you can do this.

MyString& MyString::operator=(const char* rightOp)
{
 delete []stringstorage;
 stringstorage = new char[sizeof(rightOp)]
 *stringstorage = *rightOp
 return *this
}

i removed the first const because with assignment you have to change the object and so it shouldn't be const. this is also the same for the addition operator because adding is not const.

as for the error you are having with the addition operator you cannot use strcat() for members of your class. as your compiler says strcat need a char* for each term. also when you use strcat(rightOp, rightOp); you are just concating the string to itself if you want to add one string to another you can do

// decleration
MyString operator+(const MyString& rightOp);
//definition
MyString operator+(const MyString & rightOp)
{
 strcat(this->stringstorage, rightOp.stringstorage);
 return *this;
}

hope this will help you out some

Ok thanks for the help those solved a lot of my issues I have a few more overloaders to work on so I may be back. Hopefully not!

Ok I was wondering if you guys could tell me if this first one looks good I was getting no errors. The second one here is giving me to errors which seems to be using this incorrectly.

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.

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

No errors for this one so hopefully that is correct.

MyString operator+(const char* leftOp, const MyString& rightOp)

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

This operator function must be implemented as a standalone function. Make the function a friend of the MyString class.

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

MyString.cpp:102: error: request for member âstringStorageâ in âleftOpâ, which is of non-class type âconst char*â
MyString.cpp:102: error: invalid use of âthisâ in non-member function
MyString.cpp:103: error: invalid use of âthisâ in non-member function

you char* leftOp is not a MyString object so you cant use leftOp.stringstorage. to use the string just use leftOp

strcat(leftOp, this->stringstorage);

That is what I originally did and was still getting errors. What would the purpose of rightOp be in this as well?

You have an appending error. Your string buffer is allocated with new[] to the string length + 1 byte. BUT you are appending additional characters to that string overrunning the buffer.

You need to allocate a new buffer large enough for both strings plus one terminator. Copy old string to new buffer. Concatenate new string. Delete old buffer. Assign new buffer to class string pointer. Adjust data member that contains the string length to reflect the new length!

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.