C++ 错误:“ostream”未命名类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26478680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:36:55  来源:igfitidea点击:

error: ‘ostream’ does not name a type

c++operator-overloadingoperators

提问by user2741941

I am overloading the << and >> operator in C++, but it cannot compile.

我在 C++ 中重载了 << 和 >> 运算符,但它无法编译。

The error message is :" error: ‘ostream' does not name a type" Why do I got this error? How to fix it?

错误消息是:“错误:'ostream' 未命名类型” 为什么我会收到此错误?如何解决?

#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

回答by P0W

Use std::ostreamand std::istreameverywhere.

使用std::ostreamstd::istream无处不在。

ostreamand istreamare in namespace std

ostream并且istream在命名空间中std

回答by Vlad from Moscow

Us qualified names for types defined in namespace std

命名空间 std 中定义的类型的限定名称

friend std::ostream& operator<<(std::ostream& out,const Complex& c);

It would be also better to include <iostream>instead of two separate headers <istream>and <ostream>

最好包括<iostream>而不是两个单独的标题<istream><ostream>

回答by Lucca Psaila

You forgot to add

你忘了添加

using namespace std;