首先是我得到的錯誤:不能超載<<這一切操作
error: overloaded 'operator<<' must be a binary operator (has 3 parameters) std::ostream& operator<< (std::ostream& os, const Dcomplex& c);
,我只是不明白爲什麼。我讀了其他幾個問題,他們都說只是添加const,但它不適合我。
所以這是我的頭文件:
#ifndef AUFGABE5_DCOMPLEX_H
#define AUFGABE5_DCOMPLEX_H
class Dcomplex {
private:
double re, im;
public:
Dcomplex(double r = 0, double i = 0);
Dcomplex(const Dcomplex& kopie);
~Dcomplex();
double abswert() const;
double winkel() const;
Dcomplex konjugiert() const;
Dcomplex kehrwert() const;
Dcomplex operator+(const Dcomplex& x)const;
Dcomplex operator-();
Dcomplex operator*(const Dcomplex& x)const;
Dcomplex operator-(const Dcomplex& x)const;
Dcomplex operator/(const Dcomplex& x)const;
Dcomplex& operator++();
Dcomplex& operator--();
const Dcomplex operator++(int);
const Dcomplex operator--(int);
std::ostream& operator<< (std::ostream& os, const Dcomplex& c);
std::istream& operator>> (std::istream& is, const Dcomplex& c);
bool operator>(const Dcomplex &x)const;
void print();
};
#endif //AUFGABE5_DCOMPLEX_H
我感謝爲什麼它不工作的想法。
編輯:
std::istream& Dcomplex::operator>>(std::istream &is, const Dcomplex& c) {
double re,im;
std::cout << "Der Realteil betraegt?"; is >> re;
std::cout << "Der Imaginaerteil betraegt?"; is >> im;
Dcomplex(re,im);
return is;
}
'的operator <<'和'操作者>>'必須爲非成員函數。 – songyuanyao