我正在重载 C++ 中的 << 和 >> 运算符,但它无法编译。
错误信息是:“error: ‘ostream’ does not name a type” 为什么会出现此错误?如何解决?
#ifndef COMPLEX_H
#define COMPLEX_H
#include <cstdlib> //exit
#include <istream>
#include <ostream>
class Complex{
public:
Complex(void);
Complex(double a, double b);
Complex(double a);
double real() const{
return a;
}
double imag() const{
return b;
}
friend ostream& operator<<(ostream& out,const Complex& c);
friend istream& operator>>(istream& in, Complex& c);
private:
double a;
double b;
};
ostream& operator<<(ostream& out,const Complex& c){
double a=c.real() , b = c.imag();
out << a << "+" << b<<"i";
return out;
}
istream& operator>>(istream& in, Complex& c){
in >> c.a>> c.b;
return in;
}
#endif
最佳答案
到处使用 std::ostream
和 std::istream
。
ostream
和 istream
位于命名空间 std
关于c++ - 错误 : ‘ostream’ does not name a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478680/