Hassan Gillani
Hassan Gillani

Reputation: 43

Incomplete Type can not be Defined

Hello Guys i am Learning Operator Overloading and for practice i was writing code to add complex numbers.

i seems i have done all the steps properly but in main when i create an object of my Class then i says

E:\Opp\Practice\ComplexNumbers\main.cpp|9|error: aggregate 'Complex c2' has incomplete type and cannot be defined|

E:\Opp\Practice\ComplexNumbers\main.cpp|9|error: variable 'Complex c2' has initializer but incomplete type|

You can Have a Look at My Code

#include <iostream>

using namespace std;

class Complex;
int main()
{

    Complex c1(10,20),c2(30,40),c3;
    c3=c1+c2;
    c3.Display();

    return 0;
}

class Complex
{

public:
    Complex();
    Complex(int,int);
    void setReal(int );
    void setImaginary(int );
    int getReal();
    int getImaginary();
    Complex operator + (Complex );
    void Display();

private:
    int real , imaginary;
};

Complex::Complex()
{
    real = 0;
    imaginary =0;
}


Complex::Complex(int r , int i)
{

    real = r;
    imaginary =i;
}
Complex Complex:: operator +(Complex num1)
{

    Complex temp;
    temp.real = num1.real + real;
    temp.imaginary=num1.imaginary + imaginary;
    return temp;
}

void Complex :: Display()
{
    cout << "Real " << real << "Imaginary " << imaginary << endl;
}

int Complex ::getReal()
{
    return real;
}
int Complex ::getImaginary()
{
    return imaginary;
}

void Complex ::setReal( int r)
{
    real = r;
}

void Complex::setImaginary(int i)
{
    imaginary = i;
}

Upvotes: 2

Views: 1864

Answers (1)

jpo38
jpo38

Reputation: 21514

You must move int main() after Complex class was declared. Forward declaration is not enough here.

Forward declaration (class Complex;) only let's you manipulate pointers and references (it tells the compiler the class exists but will be defined later). It does not permit you to create objects (which is what your main function tries to do...this code must be compiled after class Complex {...}; statement).

Upvotes: 1

Related Questions