Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::fstream已使用,但编译器抛出错误_C++_Io - Fatal编程技术网

C++ std::fstream已使用,但编译器抛出错误

C++ std::fstream已使用,但编译器抛出错误,c++,io,C++,Io,我用过 using std::fstream; 并尝试创建流类的对象 错误:'ofstream'未在此范围内声明 但当使用std::of stream时,它可以正常工作。我的问题是为什么使用std::fstream是不够的,因为流/ifstream的类是这个库的类 亲爱的尊敬的下议院投票者,请在下议院投票者之前提供一个理由,以便我能够改进。谢谢 您正在混合使用#include和using#includeincludes(不是吗?)在源文件中声明,使它们可用#include实际上声明了st

我用过

   using std::fstream;
并尝试创建流类的对象

错误:'ofstream'未在此范围内声明

但当使用std::of stream时,它可以正常工作。我的问题是为什么使用std::fstream是不够的,因为流/ifstream的类是这个库的类


亲爱的尊敬的下议院投票者,请在下议院投票者之前提供一个理由,以便我能够改进。谢谢

您正在混合使用
#include
using
<代码>#includeincludes(不是吗?)在源文件中声明,使它们可用<代码>#include实际上声明了
std::fstream
std::ofstream

另一方面,
使用
从当前名称空间中的其他名称空间导入声明,以允许使用非限定名称。这里
std::ofstream
是限定名(包括名称空间),非限定名是<代码>流的

因此,如果您想使用流的非限定名称
,请使用std::ofstream添加
到您的代码

<>但注意,名称空间被添加到C++中,允许声明声明的正确分离,避免名称冲突。过度使用
特别是在主命名空间中使用
指令(不赞成
使用命名空间std;
)会造成命名空间污染,并在某一时刻导致冲突


或者,您可以省略
using
指令,并在整个代码中始终使用
std::of stream

fstream
of stream
不是一回事。@user975989请详细说明。当您使用
using
指令时,您需要准确地命名类型。这两种类型不是一个,也不是一个。把它放在问题的上下文中,
使用::fstream
不使用库,而是使用。我想的是,当我“使用”fstream时,我认为ofstream所需的材料也被导入,因为所有内容都在fstream标题下,并且使用fstream必须将ofstream作为子集。错了吗?