I was writing a code for operator overloading and when i was writing i was getting a constant ambiguity error whenever I used the name of the class outside the class.It said that the name is ambiguous,and i have tried many things but couldnt overcome this error. I even defined all the functions and memebers inside the class only which i rarely do, just so that tha name of class is not used, but still for creating objects name has to be used.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class complex
{
int real, img;
public:
complex();
complex(int real, int img)
{
this->real = real;
this->img = img;
}
void print(void)
{
cout << "The sum is : " << real << "+ i" << img;
}
complex operator+(complex const &a)
{
complex b;
b.real = real + a.real;
b.img = img + a.img;
return b;
}
complex operator-(complex const &a)
{
complex b;
b.real = real - a.real;
b.img = img - a.img;
return b;
}
complex operator*(complex const &a)
{
complex b;
b.real = real * a.real;
b.img = img * a.img;
return b;
}
complex operator/(complex const &a)
{
complex b;
b.real = real / a.real;
b.img = img / a.img;
return b;
}
};
int main()
{
int real[2], img[2];
cout << "Enter the values for real and imaginary parts of first number : ";
cin >> real[1] >> img[1];
cout << "Enter the values for real and imaginary parts of second number : ";
cin >> real[2] >> img[2];
complex a(real[1], img[1]), b(real[2], img[2]), c;
c = a + b;
c.print();
c = a - b;
c.print();
c = a * b;
c.print();
c = a / b;
c.print();
return 0;
}
I even defined all the functions and memebers inside the class only which i rarely do, just so that tha name of class is not used and so it would not give me any error, but still for creating objects name has to be used and thus the error still remains.