2016-03-24 19 views
0

私はクラス内の以下の機能があります。これは、COORDSクラスですクラスオブジェクトの操作方法C++

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

を、これはありません「私はエラーを取得する< <演算子?:

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; 
} 

をオーバーロードするための正しい方法ですオペレータ< <の一致」

+1

あなたは[良い初心者の本](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)が必要なようです。表示されているコードに –

+0

があります。オブジェクトがありません。そのメソッドを呼び出す場合は、まずこのクラスのオブジェクトを作成する必要があります。 – user463035818

答えて

0

アイブ氏はそれを考え出した:

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; 
}