Operator Overloading

Any advice appreciated:

Overload the << operator for the Window class-- i.e., write a nonmember ostream-returning function that accepts a reference to an ostream object and a constant reference to a Window object and sends the following to the ostream: 'a (width x height) window' (without the quotes and with width and height replaced by the actual width and height of the window.

Here is my code:

#include <iostream>

using namespace std;

class Window
{
public:
friend ostream &operator << (ostream &,const Window &); //I think this is the line that produces errorneous results.
Window(int w, int h) : width(w), height(h) {}
private:
int width, height;
};
What's the problem you are having? You say there is an error but don't say what it is.
Remarks:
     ⇒     
/usr/bin/ld: Undefined
               symbols:
operator               std::char_traits >&, Window const&)
collect2: ld
               returned 1 exit status

I'm using an online compiler.
I couldn't figure out this question neither. Did you have any luck solving this question yet?
I would do somethign like this:
1
2
3
4
ostream& operator<< ( ostream& stm, const Window& win )
    {
            return stm<<"a ("<<win.width<<"x"<<win.height<<") window";
                }

Try it out.
Topic archived. No new replies allowed.