2015-12-15 297 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

相關問題