I have read through Should operator<< be implemented as a friend or as a member function? and Overloading Insertion Operator in C++, looks like the similar problem, but didn't fix my own problem.
My header file:
using namespace std;
class Animal {
private:
friend ostream & operator<< (ostream & o, Dog & d);
int number;
public:
Animal(int i);
int getnumber();
};
ostream & operator<< (ostream & o, Dog & d);
My cpp:
using namespace std;
int Animal::getnumber(){
return number;
}
ostream & Animal::operator<< (ostream & o, Dog & d){
//...
}
Animal::Animal(int i) : number(i){}
Implementation is simple, but I am getting the error: in cpp - Error: class "Animal" class has no member "operator<<". I don't really get it because I already declared insertion operator as a friend in Animal, why am I still getting this error? (put the ostream in public doesn't help)
It's not a member of the Animal
class, nor should it be. So don't define it as one. Define it as a free function by removing the Animal::
prefix.
ostream & operator<< (ostream & o, Dog & d){
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With