2015-06-21 110 views
0

所以我在C++新手,我一直在互聯網上關於如何做到這一點閒逛,到目前爲止,我有這樣的:從文件中讀取輸入

void includeFile(string name){ 
    ifstream ifs; 
    ifs.open(name); 
    string commands; 
    while (getline(ifs,commands)){ 
     commandReader(ifs); 
    } 
    ifs.close(); 
} 

(commandReader是一個函數,需要一個istream)

當我嘗試編譯時,我得到錯誤「沒有匹配函數的調用」,然後給我行號爲ifs.open(名稱)行。我已經包括fstream,所以不知道爲什麼它這樣做

+0

未來,請嘗試[參考](http://en.cppreference.com/w/cpp/io/basic_ifstream/open)。請注意,函數有兩個重載,其中一個重載在C++ 11中可用。 – chris

回答

1

對不起,沒關係;我發佈這個後立刻找到了答案。 的解決方案是具有name.c_string()作爲代替參數,如繩支承體C++只是加入11

0

正如@克里斯指出的,預C++ 11,ifs.open期望一個char*,不一個std::string。嘗試ifs.open(name.c_str())