【问题标题】:"Incomplete type"-error when trying to open ifstream“不完整的类型” - 尝试打开 ifstream 时出错
【发布时间】:2020-02-02 17:44:04
【问题描述】:

我正在尝试使用 Visual Studio 2019 的内置编译器读取 C++ 中的文件。按照我找到的一些示例,我尝试这样做:

#include <iostream>
using namespace std;

int main() {
    ifstream file("test.txt");
    string input = "";
    while (file >> input) {
        std::cout << input;
    }
    std::cout << "Hello World!\n";
}

但是,在编译 Visual Studio 时显示错误 incomplete type is not allowed。输出窗口显示error C2079: 'file' uses undefined class 'std::basic_ifstream&lt;char,std::char_traits&lt;char&gt;&gt;'

像这样关注this thread

ifstream file;
file.open("test.txt");

给了我同样的错误。

  1. 错误是什么意思?
  2. 如何解决?

【问题讨论】:

    标签: c++ input io header-files


    【解决方案1】:

    您需要另外包含两个标题

    #include <fstream>
    #include <string>
    

    注意那个而不是这个声明

    string input = "";
    

    你可以写

    string input;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2013-10-06
      相关资源
      最近更新 更多