不能重载 << 运算符
Cannot overload << operator
首先这是我遇到的错误:
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;
}
运算符的第一个参数是"this",所以如果你声明两个参数,运算符实际上会得到三个-"this",和你声明的两个参数。似乎您打算将其声明为 friend
:
friend ostream& operator<<(ostream& os, const Dcomplex& c);
如果在 class 中编写常规运算符覆盖函数,函数的第一个参数始终是 class 本身。您不能指定另一个。没有接受 3 个参数的运算符(?:
除外,但您不能覆盖它)。如果你想写一个第一个参数不是 class 本身,你可以试试友元函数。
class Dcomplex {
// some stuff
friend std::ostream& operator<<(std::ostream& os, const Dcomplex& c);
friend std::istream& operator>>(std::istream& is, Dcomplex& c);
}
std::ostream& operator>>(std::ostream& os, const Dcomplex& c){
// Define the output function
}
std::istream& operator>>(std::istream& is, Dcomplex& c){
// Define the input function
}
首先这是我遇到的错误:
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;
}
运算符的第一个参数是"this",所以如果你声明两个参数,运算符实际上会得到三个-"this",和你声明的两个参数。似乎您打算将其声明为 friend
:
friend ostream& operator<<(ostream& os, const Dcomplex& c);
如果在 class 中编写常规运算符覆盖函数,函数的第一个参数始终是 class 本身。您不能指定另一个。没有接受 3 个参数的运算符(?:
除外,但您不能覆盖它)。如果你想写一个第一个参数不是 class 本身,你可以试试友元函数。
class Dcomplex {
// some stuff
friend std::ostream& operator<<(std::ostream& os, const Dcomplex& c);
friend std::istream& operator>>(std::istream& is, Dcomplex& c);
}
std::ostream& operator>>(std::ostream& os, const Dcomplex& c){
// Define the output function
}
std::istream& operator>>(std::istream& is, Dcomplex& c){
// Define the input function
}