hi
I have a base class called shape and 3 drived classes: line, rect, circle.
in addition I have drawing class that contins array of shape*.
I successfully overloaded opertor << for line, rect and circle and now i try to overload << for drawing class. I tried the following:
Code:
ostream& operator << (ostream& os, const Drawing& drw)
{
for(int i=0; i<drw.size_;i++)
if (!(drw.drawing_[i].is_empty()))
os << drw.drawing_[i]<<endl;
return os<<endl;
}
and I got this error:
no match for operator << in 'os<<*shape'
so I tried casting:
Code:
...
switch ( int(drw.drawing_[i].type()) ){
case 1:
os << dynamic_cast<Line*>(&drw.drawing_[i]) <<endl;
break;
case 2:
os << dynamic_cast<Rect*>(&drw.drawing_[i]) <<endl;
break;
case 3:
os << dynamic_cast<Circle*>(&drw.drawing_[i]) <<endl;
break;
}
...
and then I got:
error: cannot dynamic cast shape* to Line* (source is not polymorphic)
any ideas??