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)
operator<<
a friend function, have it delegate to a (possibly virtual) member function:ostream& operator<<(ostream& o, Dog& d) { d.print_to_stream(o); }
– Rehearse