У меня есть домашнее задание по информационным технологиям в школе, но я столкнулся с проблемой cout. Программа должна вычислять комплексные числа.
#include <iostream>
#include <cmath>
#include <complex>
using namespace std;
class Complex
{
public:
Complex()
{
Re = 0;
Im = 0;
}
Complex(float r, float i)
{
Re = r;
Im = i;
}
void SetRe(float n)
{
Re = n;
}
float GetRe()
{
return Re;
}
void SetIm(float n)
{
Im = n;
}
float GetIm()
{
return Im;
}
float GetR()
{
float r;
float ar = pow(Re, 2);
float br = pow(Im, 2);
r = sqrt(ar + br);
return r;
}
float GetPhi()
{
float phi;
phi = atan2(Im, Re);
phi = phi * 180 / 3.1415;
return phi;
}
Complex add(Complex s2)
{
Complex sum(Re + s2.Re, Im + s2.Im);
return sum;
}
private:
float Re;
float Im;
};
int main()
{
float im = 0;
float re = 0;
cout << " Enter Real: ";
cin >> re;
cout << " Enter Imag: ";
cin >> im;
for (int i = 0; i < 30; i++)
{
cout << "=";
}
cout << endl;
Complex z1 = Complex(re, im);
cout << " z1: " << z1.GetRe() << " + " << z1.GetIm() << "i" << endl;
cout << " r: " << z1.GetR() << endl;
cout << " Phi: " << z1.GetPhi() << endl;
cout << " Enter Real: ";
cin >> re;
cout << " Enter Imag: ";
cin >> im;
Complex z2 = Complex(re, im);
Complex z;
cout << z1.add(z2); //This gives the error
cout << " z2: " << z2.GetRe() << " + " << z2.GetIm() << "i" << endl;
cout << " r: " << z2.GetR() << endl;
cout << " Phi: " << z2.GetPhi() << endl;
cout << endl;
}
Я хочу, чтобы отображалась функция z1.add(z2);
, но Xcode говорит:
Недопустимые операнды для двоичного выражения ('std :: __ 1 :: ostream' (aka 'basic_ostream ‹char>') и 'Complex')
Если удалить cout << z1.add(z2);
, все работает нормально.
std::cout << z1.add(z2).sum();
Какой метод суммы просто возвращает Re + Im. - person Nina   schedule 11.10.2019