I'm trying to understand the concept of operator overloading by writing some simple, silly tests. I thought this might be useful as this helps me understand C++ better.
Why does this example implementing a concatenation operator of Animal
class and std::string
not compile? G++ gives me the following error:
extra qualification 'Animal::' on member 'operator+' [-fpermissive]
This is the code:
#include <iostream>
using namespace std;
class Animal {
public:
string _type;
string _name;
string _sound;
Animal & Animal::operator+(const string & o);
};
Animal & Animal::operator+(const string & o) {
cout << "plus operator \n";
this->_name=o;
return *this;
}
int main( int argc, char ** argv ) {
Animal a;
a+"hhh";
cout<<a._name;
return 0;
}