Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 21, 2004 compile error: no match for function | ||||
---|---|---|---|---|
| ||||
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 |
March 22, 2004 Re: compile error: no match for function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hakki Dogusan | 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 |
March 23, 2004 Re: compile error: no match for function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Knepper | 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 |
March 24, 2004 Re: compile error: no match for function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hakki Dogusan | 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
|
Copyright © 1999-2021 by the D Language Foundation