标识符“ostream”是未定义的错误 [英] identifier "ostream" is undefined error

查看:657
本文介绍了标识符“ostream”是未定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个数字类,它支持运算符<<输出。
i有一个错误:identifierostreamis undefined从某种原因,尽管我包括,并尝试



这里的头文件:



Number.h

  #ifndef NUMBER_H 
#define NUMBER_H
#include< iostream>
class Number {
public:
//输出方法(对于所有类型继承自数字):
virtual void show()= 0;

//输出运算符:
friend ostream&运算符<< (ostream& os,const Number& f);


};

#endif

为什么编译器不能在friend函数中识别ostream?

解决方案

您需要完全限定名称 ostream 该类所在的命名空间:

  std :: ostream 
// ^^^^^



因此,您的运算符声明应为:

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

在出现未限定名称 ostream 之前使用声明:

p $ p> 使用std :: ostream;

这将允许您编写 ostream 名称没有完全限定,如在您当前版本的程序。


i need to implement a number class that support operator << for output. i have an error: "identifier "ostream" is undefined" from some reason eventhough i included and try also

here the header file:

Number.h

#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
class Number{
public:
//an output method (for all type inheritance from number):
virtual void show()=0;

//an output operator:
friend ostream& operator << (ostream &os, const Number &f);


};

#endif

why the compiler isnt recognize ostream in the friend function?

解决方案

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.

这篇关于标识符“ostream”是未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆