So I have a parent class called window, and a child class called border_window.
Now I know that friends aren't inherited but I was told that you can call the parents friend method. In this case the friend is an overloaded output operator.
supposedly that calls the parents operator posted below.
Parent class
Window& operator<<(Window& win, const string &str)
{
win.move_cursor(win.get_cursor_x(), win.get_cursor_y());
// Print each char in the string
for(int i = 0; i < str.length(); i++) {
// make sure cursor position < edge of window
if ( win.get_cursor_x() < win.get_width() ) {
win.print_char(str.at(i));
// make sure cursor position < bottom of window
} else if ( (win.get_cursor_y()+1) <= win.get_height() ) {
// cursor at edge of window,
// move to beginning of next line
win.move_cursor(0, win.get_cursor_y()+1);
win.print_char(str.at(i));
}
}
return win;
}
I get the following errors:
Error 1 error C2065: 'window' : undeclared identifier
Error 2 error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion
1) you're trying to cast to window, not Window. Remember C++ is case sensitive.
2) you're trying to cast the string into a Window, and then trying to insert the real window into the string. Basically you're doing it backwards. You want to cast 'win' and have 'win' on the left.
Although all of this seems silly, because if Border_Window is derived from Window, it will inherit Window's << operator automatically. You don't have to do this at all.