【问题标题】:C++, cannot open source file "ifstream" Visual StudioC++,无法打开源文件“ifstream”Visual Studio
【发布时间】:2015-12-15 00:33:35
【问题描述】:

我得到两种语法?即使项目成功构建也会出错。在我评论过的以下位置,我的代码的某些部分在 Visual Studio 中突出显示为红色:

#include <vector>
#include <string>
#include <iostream>
#include <ifstream> //include is highlighted// Error: cannot open source file "ifstream"

using namespace std;

class DictionarySorter{
public:

    DictionarySorter(){

    }
    void readDic(string name){
        ifstream dicFile (name); //dicFile is highlighted here// Error: incomplete type is not allowed

    }
private:
    vector<string> v;


};

【问题讨论】:

    标签: c++ ifstream


    【解决方案1】:

    std::ifstream 在标头&lt;fstream&gt; 中定义。没有标准头&lt;ifstream&gt;。

    【讨论】:

    • 我引用了一个 Youtube 教程,它使用了 ifstream 标头......你是对的。
    【解决方案2】:

    C++ ifstream 将c-string 作为打开文件名的参数。只需将ifstream dicFile(name); 中的name 更改为ifstream dicFile(name.c_str());

    您还包括一个名为 ifstream 的库,但它并不存在。 ifstream 对象位于 fstream 库中。

    【讨论】:

    • 还有一个重载接收 const std::string&amp; 所以你的答案的第一部分是无效的。
    • @clcto 嗯,那一定是 c++11 的东西。几年来我一直在学校使用 98,并且总是需要使用 .c_str() 来获得正常的 string 值。
    • 很公平,docs 确实在该构造函数上说自 c++11 以来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多