www.digitalmars.com         C & C++   DMDScript  

c++ - compile error: no match for function

reply Hakki Dogusan <dogusanh tr.net> writes:
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
parent reply Jan Knepper <jan smartsoft.us> writes:
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
parent reply Hakki Dogusan <dogusanh tr.net> writes:
Jan Knepper wrote:

 //    double X() { return x_; }
 //    double Y() { return y_; }
versus
     double 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
parent Scott Michel <scottm cs.ucla.edu> writes:
Hakki Dogusan wrote:

 Jan Knepper wrote:
 
 //    double X() { return x_; }
 //    double Y() { return y_; }
versus
     double 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.
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
Mar 24 2004