www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - compile error: no match for function

↑ ↓ ← 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
↑ ↓ 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 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
↑ ↓ 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
↑ ↓ → 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