I am trying to overload operator '=' and operator '<<' by the same method.
class Vect{
public:
//..
Vect& operator=(const Vect& a);
ostream& operator<<(ostream& out, const Vect& vect);
//..
private:
int *data;
int size;
};
this work
Vect& Vect:: operator=(const Vect& a){
//..
//copy data operator
for(int i = 0; i< size; i++){
data[i] = a.data[i];
}
return *this;
}
however: this code cause error
[Error] 'std::ostream& Vect::operator<<(std::ostream&, const Vect&)' must take exactly one argument
ostream& Vect::operator<<(ostream& out, const Vect& vect){
//.. print vect
}
I am reading "Data structure and algorithms in C++" book part (1.5.4). They said i have to use class friends for overloading '<<' operator because it is access private members data. I don't understand why. Overloading '=' operator i also access private member data without using "friend".