2011-02-12 99 views
6

我儘管這會因爲ifstream繼承工作從istream如何通過CIN或ifstream的對象作爲參數功能

string getFileContents(istream& file_contents) 
{ 
    string result; 

    string line; 
    while (getline(file_contents, line)) 
     result += line + "\n"; 

    return result; 
} 

那麼我想調用這個函數像這樣:

ifstream file_input; 
getFileContents(file_input); 
... 
getFileContents(cin); 

,但我得到visual studio中出現此錯誤:

'getFileContents':無法將參數1從std::istream轉換爲std::ifstream &

回答

7

它應該工作;你確定你沒有留下一個參數類型爲ifstream &而不是istream &的錯誤原型嗎?

+0

hah woops ..是的原型是錯的。我的錯。 – ladookie 2011-02-12 21:48:27