In below program I am getting 2 errors at below lines.
1. r = sum(p,q); //Function sum should have a prototype.
2. r = sum(p,q); //Cannot convert int to complex
Kindly advise the changes in the code.
Note: I have to do the code by passing objects of complex class to add and also the addition should return a complex number.
#include<iostream.h>
#include<conio.h>
class Complex
{
private:
int real;
int imag;
public:
void getNo()
{
cout<<"Enter real part : "<<endl;
cin>>real;
cout<<"Enter imaginary part : "<<endl;
cin>>imag;
}
Right now, you have a sum as a member function ("method") of class Complex. That means that the function needs to be called through an object of type Complex.
1 2 3 4
Complex p, q, r;
Complex my_complex_obj;
Complex r = my_complex_obj.sum(p, q);
This would solve your problem, but this isn't really an intuitive way to use your sum function, because the my_complex object isn't actually being used in sum.
My preferred workaround would be to make sum be a non-member, friend function of Complex.