Hey everyone, my first post in here. I'm having some serious problem with a program I'm making. I want to overload the <<, using ostream, and I'm having problems with it in the derived classes I'm using.
This is my base class (I've only put the operations related to this problem, and I'll do the same with the next class):
When I compile this class, it tells me there's a problem with ostream & operator<< and I get an enourmous wall of text. Does anyone have any idea how to solve this?
It appears you are attempting to have the derived class' operator<< output the base class members, which is possible, but only if the base class members are public or protected.
Probably the better way to write the derived class' operator<< is like this:
1 2 3 4
ostream& operator<<( ostream& os, const Utente& utent )
{
return os << static_cast<const Pessoa&>( *this ) << /* derived class members */;
}
Unfortunately operator overloading and polymorphism combined don't exactly lend themselves to nice code; the cast is necessary to call the base class' operator<<.
The base class member-atributes are protected and the member-functions are public.
The code you wrote is giving me this error: invalid use of "this" in non member function.
Any ideas how to solve this?
By the way, I only intend to output in the program itself the derived class members, but for that I need to output the base class members. At least that's what I think.
It's a float. consulta_efect and consulta_marc are vector<Consulta>.
I've just noticed something. Utente is dependent on Consulta, and Consulta is dependent on Utente. I've solved this cyclical dependency by writing a forward declaration in these two classes. But I wonder if this is the cause of the problem.