我有这个功能

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

而这两个
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);
}

你能告诉我为什么编译器抱怨
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>&}’

作为ifstream类从istream继承?还是我在这里错了?

最佳答案

我认为最可能的原因是缺少<fstream>头文件。如果您所包含的只是<iosfwd>,那么编译器将不会知道ifstream是从istream继承的。

关于c++ - 将ifstream对象传递给需要istream类型的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12857775/

10-11 20:38