identifier “ostream” is undefined error [closed]

余生颓废 提交于 2019-11-30 19:51:55

You need to fully qualify the name ostream with the name of the namespace that class lives in:

    std::ostream
//  ^^^^^

So your operator declaration should become:

friend std::ostream& operator << (std::ostream &os, const Number &f);
//     ^^^^^                      ^^^^^

Alternatively, you could have a using declaration before the unqualified name ostream appears:

using std::ostream;

This would allow you to write the ostream name without full qualification, as in your current version of the program.

Andy Prowl's answer is great but please resist putting "using std::ostream" in a header. If you do this then other compilation units using your header file will also have this namespace 'used' by default and you can get nasty compilation errors with namespace clashes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!