c++ifstream未声明的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5931971/
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 19:12:29  来源:igfitidea点击:

c++ ifstream undeclared identifier

c++visual-studio

提问by rach

Hey guys, I'm in Visual Studio and am getting 'ifstream undeclared identifier' with this code (same for ofstream)

嘿伙计们,我在 Visual Studio 中,正在使用此代码获取“ifstream 未声明标识符”(与 ofstream 相同)

#include <iostream>
#include <iomanip>
#include <fstream>
void main()
{
    ifstream infile("file.txt");
    ofstream outfile("out.txt");
}

do I need to include something else?

我需要包括其他东西吗?

回答by RageD

You need to scope it. Use using namespace std;or preface ifstreamand ostreamwith std::

你需要确定它的范围。使用using namespace std;或前言ifstreamostreamstd::

For example, std::ifstream

例如, std::ifstream

Currently, the compiler does not know where these structures are defined (since they are declared/defined within the stdnamespace). This is why you need to scope your structures/functions in this case.

目前,编译器不知道这些结构是在哪里定义的(因为它们是在std命名空间中声明/定义的)。这就是为什么您需要在这种情况下确定结构/功能的范围。

回答by jwismar

You need to reference the standard namespace (std). Try this:

您需要引用标准命名空间 (std)。尝试这个:

#include <iostream>
#include <iomanip>
#include <fstream>
void main()
{
    std::ifstream infile("file.txt");
    std::ofstream outfile("out.txt");
}

回答by Ram Mokkapati

You can use

您可以使用

using namespace std;

instead of prefixing everyline with std::

而不是在每一行前面加上 std::