2016-03-24 21 views
0

Ben bir sınıf içerisindeki aşağıdaki fonksiyon var:Bir sınıf nesnesinde işlemler nasıl yapılır? C++

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

Bu, Coords sınıftır bu hayır" bir hata alıyorum < < operatörü ?:

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

aşırı doğru yolu var mı operatör < < için maç "

+1

[Yeni başlayanlar kitabı] 'na ihtiyacınız var gibi görünüyor (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –

+0

gösterdiğiniz kodda hiçbir nesne yoktur. Yöntemlerini çağırmak istiyorsanız, öncelikle bu sınıfın bir nesnesini oluşturmanız gerekir. – user463035818

cevap

0

Ive bunu anladım:

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; 
} 
İlgili konular