【问题标题】:How can I make the c++ fstream class references compile in Visual Studio 2010 CLR如何使 c++ fstream 类引用在 Visual Studio 2010 CLR 中编译
【发布时间】:2011-12-09 18:27:48
【问题描述】:

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

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

class Test_CLR
{
    int openFlag;

    int isOpen(void)
    {
        return openFlag;
    };

    fstream testFile;
};

【问题讨论】:

    标签: c++ visual-studio-2010 developer-tools


    【解决方案1】:

    fstream 在 std 命名空间中。改用 std::fstream 。欲了解更多信息,请参阅http://www.cplusplus.com/doc/tutorial/namespaces/

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

    【讨论】:

      【解决方案2】:

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

      基本上你可以通过三种方式让 fstream 类使用 std 命名空间:

      方法 1

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

      #include <string.h>
      #include <fstream>
      
      using namespace std;   //ADDED CODE
      
      class Test_CLR
      {
          int openFlag;
      
          int isOpen(void)
          {
              return openFlag;
          };
      
          fstream testFile;
      };
      

      方法 2

      声明整个程序仅使用 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
      };
      

      在顶部发布的链接中解释了差异。任君挑选:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        • 1970-01-01
        • 2011-04-28
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多