public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Copy Constructor Problem with 3.2.2??
@ 2004-06-18 11:01 Phil Prentice
  0 siblings, 0 replies; 5+ messages in thread
From: Phil Prentice @ 2004-06-18 11:01 UTC (permalink / raw)
  To: gcc-help, gcc


  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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Copy Constructor Problem with 3.2.2??
  2004-06-18 16:20 ` Joe Buck
@ 2004-06-18 17:29   ` Rupert Wood
  0 siblings, 0 replies; 5+ messages in thread
From: Rupert Wood @ 2004-06-18 17:29 UTC (permalink / raw)
  To: 'Joe Buck', 'Phil Prentice'; +Cc: gcc-help, gcc

Joe Buck wrote:

> > 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.
> Then you should send a bug report to Microsoft.  If the compiler
> accepts an attempt to bind a non-const reference to a temporary,
> this is a serious error in the compiler.

It's a documented extension. The example does raise a level 4 warning

   http://msdn.microsoft.com/library/en-us/vccore/html/C4239.asp

and won't compile if you turn off extensions with flag '/Za'.

Rup.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Copy Constructor Problem with 3.2.2??
       [not found] <E1BbHDo-0007oa-4a@monty-python.gnu.org>
  2004-06-18 11:17 ` Giovanni Bajo
@ 2004-06-18 16:20 ` Joe Buck
  2004-06-18 17:29   ` Rupert Wood
  1 sibling, 1 reply; 5+ messages in thread
From: Joe Buck @ 2004-06-18 16:20 UTC (permalink / raw)
  To: Phil Prentice; +Cc: gcc-help, gcc

On Fri, Jun 18, 2004 at 11:03:49AM +0100, Phil Prentice wrote:
>   I have a problem with the copy constructor.  

Point::Point(Point&) is not a copy constructor (or, rather, it is a
restricted copy constructor); it only accepts arguments that are
actual, changeable Point objects.

> 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.

Then you should send a bug report to Microsoft.  If the compiler
accepts an attempt to bind a non-const reference to a temporary,
this is a serious error in the compiler.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Copy Constructor Problem with 3.2.2??
       [not found] <E1BbHDo-0007oa-4a@monty-python.gnu.org>
@ 2004-06-18 11:17 ` Giovanni Bajo
  2004-06-18 16:20 ` Joe Buck
  1 sibling, 0 replies; 5+ messages in thread
From: Giovanni Bajo @ 2004-06-18 11:17 UTC (permalink / raw)
  To: Phil Prentice, gcc-help, gcc

Phil Prentice wrote:

>     Point (Point& pt);          // Copy Constructor

Point (const Point& pt);

Giovanni Bajo


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Copy Constructor Problem with 3.2.2??
@ 2004-06-18 11:15 Jyotirmoy Das
  0 siblings, 0 replies; 5+ messages in thread
From: Jyotirmoy Das @ 2004-06-18 11:15 UTC (permalink / raw)
  To: Phil Prentice, gcc-help, gcc

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-06-18 17:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-18 11:01 Copy Constructor Problem with 3.2.2?? Phil Prentice
2004-06-18 11:15 Jyotirmoy Das
     [not found] <E1BbHDo-0007oa-4a@monty-python.gnu.org>
2004-06-18 11:17 ` Giovanni Bajo
2004-06-18 16:20 ` Joe Buck
2004-06-18 17:29   ` Rupert Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).