Mam kod dla liczb zespolonych (ucze sie obiektowości). Wiem, że mnożenie jest nipoprawne, ale nie o to teraz chodzi - pytanie jest inne. Mam kod, i on działa:
#include <iostream>
#include <ostream>
using namespace std;
class complex {
protected:
double Re, Im;
public:
complex() : Re(0.0), Im(0.0) {} // konstruktor bezargumentowy
complex(double Re, double Im) : Re(Re), Im(Im) {} // konstruktor z dwoma argumentami
double getRe() const { return Re; }
double getIm() const { return Im; }
friend complex operator+(const complex&, const complex&); // funkcja zaprzyjaźniona
friend ostream& operator<<(ostream&, const complex&); // funkcja zaprzyjaźniona
complex operator*(const double d)
{
return complex( this->Re * d, this->Im * d);
}
complex& operator=(const complex& s)
{
this->Re = s.Re;
this->Im = s.Im;
return *this;
}
};
ostream& operator<<(ostream& out, const complex &a) {
out << "(" << a.getRe() << ", " << a.getIm() << ")" << endl;
return out;
}
int main()
{
complex c(3,4);
cout << c;
c = c * 2;
cout << c;
}
Jednak jak napiszę:
c = 2*c;
dostaję błąd:
error: no match for ‘operator*’ (operand types are ‘int’ and ‘complex’)|
Co jest nie tak i czy można to poprawić, aby pisać również c = 2*c; ? (oraz dla innych działań: -,+,/)? Dziękuję!