identifier quot;ostreamquot; is undefined error(标识符“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
这里是头文件:
数字.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
为什么编译器不识别友元函数中的ostream?
why the compiler isnt recognize ostream in the friend function?
推荐答案
您需要使用类所在的命名空间的名称来完全限定名称 ostream:
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);
// ^^^^^ ^^^^^
或者,您可以在非限定名称 ostream 出现之前使用 using 声明:
Alternatively, you could have a using declaration before the unqualified name ostream appears:
using std::ostream;
这将允许您在没有完全限定的情况下编写 ostream 名称,就像在您当前版本的程序中一样.
This would allow you to write the ostream name without full qualification, as in your current version of the program.
这篇关于标识符“ostream"是未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:标识符“ostream"是未定义的错误
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
