Problem with copy constructor

W

wyndz0108

Hi,

I have this working program (see first program below) that works
without an explicit copy constructor:

//--------------------------- start of first program
-----------------------------//

#include <iostream>

using namespace std;

class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex(int r, int i); // convert constructor

// overload the following operators
Complex operator - () ; // unary negation

int real;
int imag;
};

int main () {

Complex x(1,8); // input is 1+8i
Complex z ; // output should be -1 + -8i

z=-x;
cout << z.real << " + " << z.imag <<"i";
}

Complex::Complex(int r, int i): real(r), imag(i) {}

Complex Complex::eek:perator- () {
return Complex(-real, -imag);
}

// --------------------- end of first program
------------------------- //

The error occurs when I added a copy constructor (see second program
below).

//------------------- start of second program ----------------------//

#include <iostream>

using namespace std;

class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex (Complex &) ; // added copy constructor
Complex(int r, int i); // convert constructor

// overload the following operators
Complex operator - () ; // unary negation

int real;
int imag;
};

int main () {

Complex x(1,8); // input is 1+8i
Complex y(x);
Complex z ; // output should be -1 + -8i

z=-x;
cout << z.real << " + " << z.imag <<"i";
}

Complex::Complex(int r, int i): real(r), imag(i) {}

Complex::Complex(Complex & copy): real(copy.real), imag(copy.imag) {}

Complex Complex::eek:perator- () {
return Complex(-real, -imag);
}

//------------------------ end of second program
------------------------------//

Please help.

Thank you.
 
P

Paul Brettschneider

Hi,
Hi,

I have this working program (see first program below) that works
without an explicit copy constructor:
[...]
The error occurs when I added a copy constructor (see second program
below).
[...]
Complex (Complex &) ; // added copy constructor
[...]
Complex::Complex(Complex & copy): real(copy.real), imag(copy.imag) {}
[...]
        Complex z     ; // output should be -1 + -8i

        z=-x;

Try
Complex(const Complex &);
and
Complex::Complex(const Complex & copy): real(copy.real), imag(copy.imag)
{}

The reason being that the expression "-x" generates a reference to a const
object. Your compiler should have told you so. ;)

HTH,
Paul
 
A

Andrey Tarasevich

wyndz0108 said:
Hi,

I have this working program (see first program below) that works
without an explicit copy constructor:
...
The error occurs when I added a copy constructor (see second program
below).
...
class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex (Complex &) ; // added copy constructor
Complex(int r, int i); // convert constructor

// overload the following operators
Complex operator - () ; // unary negation

int real;
int imag;
};
...
Complex Complex::eek:perator- () {
return Complex(-real, -imag);
}

Your program uses the copy constructor to copy the return value of your
'operator-' function. The return value is a temporary object. Temporary objects
cannot be used as initializers for non-const references. For this reason it is
impossible to call your copy constructor. This is what's causing the error.

Declare your copy constructor as accepting a const reference parameter

Complex (const Complex &);
 
A

Andrey Tarasevich

Paul said:
...
The reason being that the expression "-x" generates a reference to a const
object.
...

Actually, the expression '-x' generates a temporary non-const object, not "a
reference to a const object". Moreover, the generated object is then passed to
the copy-assignment operator, not to the copy-constructor. This parts is fine.

The problem resides inside the definition of 'operator-'. It makes an attempt to
copy a temporary object in its 'return' statement, which is impossible with a
copy-constructor taking a non-const reference as its parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top