c++ - compile error: no match for function
- Hakki Dogusan (34/34) Mar 21 2004 Hi,
- Jan Knepper (9/48) Mar 22 2004 // double X() { return x_; }
- Hakki Dogusan (8/12) Mar 23 2004 Thanks Jan,
- Scott Michel (8/24) Mar 24 2004 Perhaps the compiler could be a little more detailed about its error
Hi, Code as follows: #include <iostream> using namespace std; class Point { public: Point(): x_(0), y_(0) { } Point(double x, double y): x_(x), y_(y) {} public: void X(double x) { x_ = x; } void Y(double y) { y_ = y; } double X() { return x_; } double Y() { return y_; } private: double x_; double y_; }; // ERROR // with const gives "no match for function X()" void print_point(const Point& p) { cout << "x: " << p.X() << " y: " << p.Y(); } int main() { Point p1(10, 20); cout << "x: " << p1.X() << " y: " << p1.Y(); // OK print_point(p1); // ERROR return 0; } -- Regards, Hakki Dogusan
Mar 21 2004
Hakki Dogusan wrote:Hi, Code as follows: #include <iostream> using namespace std; class Point { public: Point(): x_(0), y_(0) { } Point(double x, double y): x_(x), y_(y) {} public: void X(double x) { x_ = x; } void Y(double y) { y_ = y; }// double X() { return x_; } // double Y() { return y_; } double X() const { return x_; } double Y() const { return y_; }private: double x_; double y_; }; // ERROR // with const gives "no match for function X()" void print_point(const Point& p) { cout << "x: " << p.X() << " y: " << p.Y(); } int main() { Point p1(10, 20); cout << "x: " << p1.X() << " y: " << p1.Y(); // OK print_point(p1); // ERROR return 0; } -- Regards, Hakki Dogusan-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Mar 22 2004
Jan Knepper wrote:// double X() { return x_; } // double Y() { return y_; }versusdouble X() const { return x_; } double Y() const { return y_; }Thanks Jan, I see dmc uses "thread warnings as error" way!. Maybe error message could be a little detailed. -- Regards, Hakki Dogusan
Mar 23 2004
Hakki Dogusan wrote:Jan Knepper wrote:Perhaps the compiler could be a little more detailed about its error messages, but this kind of error is actually quite common. Since you're passing a "const T" object/object ref into the method, the compiler wants the method that takes a constant "this" object -- which is why you need the const qualifier on the method declaration. See the C++ FAQ, Section 18, specifically 18.9. -scooter// double X() { return x_; } // double Y() { return y_; }versusdouble X() const { return x_; } double Y() const { return y_; }Thanks Jan, I see dmc uses "thread warnings as error" way!. Maybe error message could be a little detailed.
Mar 24 2004