This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Copy Constructor Problem with 3.2.2??

Hi Phil,
  If you have a class X, then you need to define copy constructor as
X::X(const X&);

So if you change the following line:
 >>>   Point (Point& pt);          
 <<<<  Point (const Point& pt);          
Then it will work fine.


-----Original Message-----
From: [] On
Behalf Of Phil Prentice
Sent: Friday, June 18, 2004 3:34 PM
Subject: Copy Constructor Problem with 3.2.2??

  I have a problem with the copy constructor.  I have knocked up a
program that demonstrates this problem.  I have compiled this same
using Visual C++ (windows) and it compiles & runs as I would have

When I tryed compiling this same program on 3.2.2 (& 2.96) the compiler
the following error:- In function `int main()': no matching function for call to `Point::Point(Point)' candidates are: Point::Point(Point&)

There may now be good reason for this error (its been a while since I
done a lot of C++), but I'm curious as to what it is.  Of course I could

change ret to return a reference, but then thats not a good thing to do 
(return automatic variables by reference).  I could make the variable
that is 
being returned static, but again I'm not sure if thats a good thing to

Note:- the real code that demonstrates this problem is much more complex
this example;  

The code I'm porting used to compile & run on a very old version of g++
I'm trying to build it on a newer version (on Linux.).  

Of course I cant try and create a copy constructor Point::Point(Point) 
because the compiler wont let me.

Any comments would be welcome

#include <stdio.h>
class Point
    double x1;
    double y1;
    Point();                    // Default constructor
    Point (Point& pt);          // Copy Constructor
    Point ret();                // Member Function

Point::Point() // Default constructor
  x1=0.0; y1=0.0;

Point::Point (Point& pt) // Copy Constructor
  x1 = pt.x1;
  y1 = pt.y1;

Point Point::ret() // General Member function
  Point local;     // Local variable
  return local;    // Return it by value

  Point m;                      // Default constructor
  Point n=m;                    // COPY
  Point o=m.ret();              // COPY constructor, does not compile!!!
  g++ In function `int main()': no matching function for call to `Point::Point(Point)' candidates are: Point::Point(Point&)

  Thanks for your help


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]