c++ - 错误 : ‘ostream’ does not name a type

标签 c++ operator-overloading operators

我正在重载 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::ostreamstd::istream

ostreamistream 位于命名空间 std

关于c++ - 错误 : ‘ostream’ does not name a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478680/

相关文章:

c++ - 如何正确地将选择排序算法重新设计为冒泡排序?

c++ - 有没有办法在输出二进制文件中存储 clang 编译时标志?

c++ - Operator[] 重载返回错误数据

C++ 模板 : overload operator +, 而返回类型由输入类型决定

c - 表达式中的问号运算符

c++ - strcat 替代问题 C++

c++ - std::reference_wrapper<const T> 和 T 之间的最佳可行重载函数

C++:尝试使用等效的 STL 算法消除原始循环

C++ 基于文本的 RPG 库存系统

php - “=”和 “=&”在赋值变量时有什么区别?