str[0] = 'a' => how to make it work (overloading operators)
Hi!
I have a problem with overloading a [] operator. Whenever I want to assign value to a string I always get this message "error C2106: '=' : left operand must be l-value".
Example:
str[1] = 'a';
My code in .hpp file:
Code:
String &operator=(const String &secondString);
String &operator=(const char *secondString);
String &operator=(const char &character);
char operator[](int index);
Code in .cpp file:
Code:
String &String::operator=(const String &secondString) {
setString(secondString);
return *this;
}
String &String::operator=(const char *secondString) {
setString(secondString);
return *this;
}
String &String::operator=(const char &character) {
setString(character);
return *this;
}
char String::operator[](int index) {
this->setIndex(index);
return getChar(index);
}
So, what am I doing wrong?