1

在以下代码中,Visual Studio 2010 C++ 无法识别接受 fstream 包含但无法识别 fstream 类型:

#include <string.h>
#include <fstream>

class Test_CLR
{
    int openFlag;

    int isOpen(void)
    {
        return openFlag;
    };

    fstream testFile;
};
4

2 回答 2

2

您忘记指定名称空间(您可以在此处找到有关名称空间的更多信息:http ://www.cplusplus.com/doc/tutorial/namespaces/ )

本质上,您可以通过三种方式使 fstream 类使用 std 命名空间:

方法一

声明整个文件以使用 std 命名空间:

#include <string.h>
#include <fstream>

using namespace std;   //ADDED CODE

class Test_CLR
{
    int openFlag;

    int isOpen(void)
    {
        return openFlag;
    };

    fstream testFile;
};

方法二

声明整个程序只使用std 命名空间中的fstream :

#include <string.h>
#include <fstream>

using std::fstream;   //ADDED CODE

class Test_CLR
{
    int openFlag;

    int isOpen(void)
    {
        return openFlag;
    };

    fstream testFile;
};

方法 3

声明一次使用 fstream 以链接到 std 命名空间:

#include <string.h>
#include <fstream>

class Test_CLR
{
    int openFlag;

    int isOpen(void)
    {
        return openFlag;
    };

    std::fstream testFile;   //ADDED CODE
};

差异在顶部发布的链接中进行了说明。随便挑吧:)

于 2011-12-10T01:22:09.430 回答
2

fstream 在 std 命名空间中。改用 std::fstream 。有关详细信息,请参阅http://www.cplusplus.com/doc/tutorial/namespaces/

此外,您可以使用 'using' 关键字允许在不同范围内使用类型,有关更多信息,请参阅http://www.cprogramming.com/tutorial/namespaces.html

于 2011-12-09T18:43:06.007 回答