Search code examples
c++inheritanceifstreamistream

Passing ifstream object to a function that requires an istream type


I have this function

void StrType::saveString(bool skip, InType charsAllowed, std::istream& inStream)
{ //something cool 
};

and these two

void StrType::getString(bool skip, InType charsAllowed)
{
    saveString(skip, charsAllowed, std::cin);
}
void StrType::getStringFile(bool skip, InType charsAllowed,  std::ifstream& inFile)
{
    saveString(skip, charsAllowed, inFile);
}

Can you tell me why is the compilator complaining that

strtype.cpp: In member function ‘void StrType::getStringFile(bool, InType, std::ifstream&)’:
strtype.cpp:42:39: error: no matching function for call to ‘StrType::saveString(bool&, InType&, std::ifstream&)’
strtype.cpp:42:39: note: candidate is:
strtype.cpp:9:6: note: void StrType::saveString(bool, InType, std::istream&)
strtype.cpp:9:6: note:   no known conversion for argument 3 from ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘std::istream& {aka std::basic_istream<char>&}’

as the ifstream class inherits from istream? Or am I wrong here?


Solution

  • I think the most likely cause is a missing <fstream> header file. If all you have included is <iosfwd> then the compiler won't know that ifstream inherits from istream.