Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass cin or ifstream object as argument function

Tags:

c++

I though this would work since ifstream inherits from istream

string getFileContents(istream& file_contents)
{
    string result;

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

    return result;
}

then I want to call this function like so:

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

but I get this error in visual studio:

'getFileContents' : cannot convert parameter 1 from std::istream to std::ifstream &

like image 485
ladookie Avatar asked Feb 12 '11 21:02

ladookie


1 Answers

It should work; are you sure you didn't leave around a wrong prototype that has a parameter of type ifstream & instead of istream &?

like image 99
Matteo Italia Avatar answered Nov 16 '22 19:11

Matteo Italia