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!