0

I have the following function inside a class:

void solveMaze::getLoc() {
    mouse m;
    x = m.x;
    y = m.y;
    coords c(x, y);
    cout << c << endl;
}

This is Coords class, Is this the right way to overload the << operator?:

class coords {
    public:
        coords(int, int);
        int x;
        int y;
        coords& operator<<(const coords& rhs);
};


coords& coords::operator<<(const coords& rhs) {
    cout << x << " " << y << endl;
}

coords::coords(int a, int b) {
    x = a;
    y = b;
}

I get an error "no match for operator<<"

nanjero
  • 13
  • 2

1 Answers1

0

Ive figured it out:

class coords {
    public:
        coords();
        coords(int, int);
        int x;
        int y;
        friend ostream &operator<<(ostream &output, const coords &c);
};


ostream &operator<<(ostream &output, const coords &c) {
    output << c.x << " " << c.y;
    return output;
}

coords::coords(int a, int b) {
    x = a;
    y = b;
}
nanjero
  • 13
  • 2