This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Copy Constructor Problem with 3.2.2??
- From: "Jyotirmoy Das" <Jyotirmoy_Das at infosys dot com>
- To: "Phil Prentice" <philp dot cheer at btinternet dot com>,<gcc-help at gnu dot org>,<gcc at gnu dot org>
- Date: Fri, 18 Jun 2004 16:40:25 +0530
- Subject: RE: Copy Constructor Problem with 3.2.2??
Hi Phil,
If you have a class X, then you need to define copy constructor as
follows:
X::X(const X&);
So if you change the following line:
>>> Point (Point& pt);
to
<<<< Point (const Point& pt);
Then it will work fine.
Regards,
Jyoti
-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Phil Prentice
Sent: Friday, June 18, 2004 3:34 PM
To: gcc-help@gnu.org; gcc@gnu.org
Subject: Copy Constructor Problem with 3.2.2??
I have a problem with the copy constructor. I have knocked up a
simple
program that demonstrates this problem. I have compiled this same
program
using Visual C++ (windows) and it compiles & runs as I would have
expected.
When I tryed compiling this same program on 3.2.2 (& 2.96) the compiler
gives
the following error:-
x.cc: In function `int main()':
x.cc:34: no matching function for call to `Point::Point(Point)'
x.cc:19: candidates are: Point::Point(Point&)
There may now be good reason for this error (its been a while since I
have
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
do?
Note:- the real code that demonstrates this problem is much more complex
then
this example;
The code I'm porting used to compile & run on a very old version of g++
and
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
CODE
=====
#include <stdio.h>
class Point
{
protected:
double x1;
double y1;
public:
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
}
main()
{
Point m; // Default constructor
Point n=m; // COPY constructor....works
Point o=m.ret(); // COPY constructor, does not compile!!!
/*
g++ x.cc
x.cc: In function `int main()':
x.cc:34: no matching function for call to `Point::Point(Point)'
x.cc:19: candidates are: Point::Point(Point&)
*/
}
Thanks for your help
Phil