c++ - “不完整类型” - 尝试打开 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");
给我同样的错误。
- 错误是什么意思?
- 我如何解决它?
解决方案
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;
推荐阅读
- vue.js - 无法让 v-img 在 v-flex 内居中
- lua - 将字符串与函数 Lua 关联
- reactjs - 找不到所需的文件。名称:index.js
- ios - Swift 结构警告“'UnsafeMutableRawPointer' 的初始化导致指针悬空”
- c++ - 模板化检查是否存在带有参数列表的类成员函数?
- hadoop - 之前停止运行(和工作)实例后无法启动 Hadoop,因为 Datanode 说目录已锁定
- html - 网页不呈现 svg
- python - Plotly Chart-Studio 配置问题以更改 Python 中的模式栏
- f# - 如何删除括号中的最后一个文本(优雅而简单)
- php - 收到错误无法修改标头信息 - 标头已经