2015-12-15 291 views
0

我得到两个语法?即使项目建立成功,也会出现错误。我的代码的某些部分突出显示为红色在Visual Studio中,我在评论以下位置:C++,无法打开源文件“ifstream”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; 


}; 

回答

7

std::ifstream在头<fstream>定义。没有标准标头<ifstream>

+0

我在引用一个Youtube教程,它使用了ifstream头....你是对的。 – haris

1

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

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

+1

还有一个重载需要一个'const std :: string&',所以你的答案的第一部分是无效的。 – clcto

+0

@clcto嗯,这必须是一个C++ 11的东西。几年来,我一直在使用98学校,并且总是需要使用'.c_str()'作为正常的'string'值。 –

+0

足够公平的,[docs](http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream)从那个构造函数的C++ 11开始说。 – clcto

相关问题