vendredi 6 octobre 2017

About Complex numbers using friend function

I am trying to add 2 complex numbers by using friend functions and constructors to initialize values but getting error 'Undefined reference to complex1::complex1()' can someone suggest where am I going wrong.

#include<iostream>
using namespace std;

class complex1
{
    float real,img;
public:
    complex1();
    complex1(float a,float b)
    {
        real=a;
        img=b;
    }
    friend complex1 sum(complex1,complex1);
    friend void display(complex1);
};

complex1 sum(complex1 c1,complex1 c2)
{
    complex1 c3;
    c3.real=c1.real+c2.real;
    c3.img=c1.img+c2.img;
    return c3;
}

void display(complex1 c)
{
    cout<<c.real<<"+j"<<c.img;
}

int main()
{
    complex1 c1(100.9,200.9);
    complex1 c2(50.9,50.9);
    complex1 c=sum(c1,c2);
    display(c);     //display and sum is given directly because it is friend
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire