Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class has no member "operator<<"

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)

like image 956
HoKy22 Avatar asked Mar 11 '13 02:03

HoKy22


1 Answers

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){
    //...
}
like image 74
Benjamin Lindley Avatar answered Oct 18 '22 09:10

Benjamin Lindley