首页 > 解决方案 > “不完整类型” - 尝试打开 ifstream 时出错

问题描述

我正在尝试使用 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<char,std::char_traits<char>>'

像这样跟随这个线程

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

给我同样的错误。

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

标签: c++inputioheader-files

解决方案


You need to include additionally two headers

#include <fstream>
#include <string>

Pay attention to that instead of this declaration

string input = "";

you could just write

string input;

推荐阅读