Question: Can someone please help me correct the problems in the following code that enables operations on complex numbers and enables input and output of complex
Can someone please help me correct the problems in the following code that enables operations on complex numbers and enables input and output of complex numbers through overloaded << and >> operators and overloads the multiplication *, division /, == and != operators. I am getting the following errors when trying to run the program:
'COMPLEX_H': macro is not defined or definition is different after precompiled header use,
'operator>>' must return a value,
header stop cannot be in a macro,
the #endif for this directive is missing,
unexpected tokens following preprocessor directive - expected newline.
#ifndef COMPLEX_H
#define COMPLEX_H
#include "stdafx.h""
#include
using namespace std;
class Complex {
private:
double real;
double imaginary;
public:
Complex(double r = 0.0, double i = 0.0)
{
real = r;
imaginary = i;
}
Complex operator*(Complex c2)
{
Complex temp;
temp.real = real * c2.real;
temp.imaginary = imaginary * c2.imaginary;
return temp;
}
Complex operator/(Complex c2)
{
Complex temp;
temp.real = ((real * c2.real) + (imaginary * c2.imaginary)) / ((c2.real * c2.real) + (c2.imaginary * c2.imaginary));
temp.imaginary = ((real * c2.real) - (imaginary * c2.imaginary)) / ((c2.real * c2.real) + (c2.imaginary * c2.imaginary));
return temp;
}
bool operator==(Complex &c) {
return real == c.real && imaginary == c.imaginary;
}
bool operator!=(Complex &c) {
return !(real == c.real && imaginary == c.imaginary);
}
friend ostream &operator<<(ostream &out, Complex &c);
friend istream &operator>>(istream &in, Complex &c);
};
ostream &operator<<(ostream &out, Complex &c)
{
if (c.imaginary < 0)
out << "(" << c.real << c.imaginary << "i )";
else
out << "(" << c.real << "+" << c.imaginary << "i )";
return out;
}
istream &operator>>(istream &in, Complex &c) {
in >> c.real >> c.imaginary;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
