【发布时间】:2017-05-06 12:30:38
【问题描述】:
首先这是我得到的错误:
错误:重载的 'operator
我只是不明白为什么。我读了其他几个问题,他们都说只添加 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<<和operator>>必须是非成员函数。
标签: c++ binary overloading operator-keyword