-2

I have declared a class below is header file:

class Complex
{
private:
    int real;
    int imaginary;

public:

    Complex(); // no arg contructor
    Complex(int,int); // 2 - args constructor
    Complex(const Complex& temp);  // copy constructor
};

No I am trying to declare copy constructor again,I know it works but wanted to have some more functionality but its not working when I am including its code in implementation file.Here is code from implementation file.

Compelx::Complex(const Complex& temp) // copy constructor 
{
    real = 2*temp.real;
    imaginary =2*temp.imaginary;
}

In the main() I have following code

Complex a,b;

a.setReal(10);
cout<<a.getReal()<<endl;

b=a; // problem is here, copy constructor(that is redefined one) is not being executed.

b.print();

Copy constructor is not executing instead I am getting following error:

1> Complex.cpp 1>Complex.cpp(21): error C2653: 'Compelx' : is not a class or namespace name 1>Complex.cpp(21): error C2226: syntax error : unexpected type 'Complex' 1>Complex.cpp(22): error C2143: syntax error : missing ';' before '{' 1>Complex.cpp(22): error C2447: '{' : missing function header (old-style formal list?) 1> main.cpp 1> Generating Code... ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Labeeb
  • 369
  • 2
  • 6
  • 18

4 Answers4

3

Looks like a spelling mistake, try replacing

Compelx::Complex(const Complex& temp) // copy constructor 

with

Complex::Complex(const Complex& temp) // copy constructor 
Alex Walker
  • 2,337
  • 1
  • 17
  • 32
3

You're using a copy-assignment operator, not a copy constructor. A copy constructor call would look like:

Complex b(a);

What you are calling has the signature:

Complex& operator=(const Complex& rhs);
Yuushi
  • 25,132
  • 7
  • 63
  • 81
2
Compelx


Complex

See the difference.

Oswald
  • 31,254
  • 3
  • 43
  • 68
2

This statement is an assignment, and you have not provided an implementation for the copy-assignment operator: Complex& operator=(const Complex&). With the copy&swap idiom you can reuse your copy-constructor to implement that operator.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283