The class does not have a member "operator <<"

I read if the operator <<be implemented as a friend or as a member function? and Overloading the insert statement in C ++ seems like a similar problem, but did not 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){} 

The implementation is simple, but I get the error: in cpp - Error: the "Animal" class does not have a member operator <<. I really don't understand it, because I already declared an input statement as a friend in Animal, why am I still getting this error? (putting publication in publication does not help)

+4
source share
1 answer

He is not a member of the Animal class, and should not be. Therefore, do not define it as one. Define it as a free function by removing the Animal:: prefix.

 ostream & operator<< (ostream & o, Dog & d){ //... } 
+7
source

All Articles