【问题标题】:Passing ifstream object to a function that requires an istream type将 ifstream 对象传递给需要 istream 类型的函数
【发布时间】:2012-10-12 11:09:52
【问题描述】:

我有这个功能

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?还是我错了?

【问题讨论】:

标签: c++ inheritance ifstream istream


【解决方案1】:

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

【讨论】:

  • 该死,你是对的。我不仅包含 fstream 标头。好难过。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 2023-03-28
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 2015-02-01
相关资源
最近更新 更多