[英]Compiler error for using operator '>>' with ifstream
我正在嘗試為IO使用自定義模板,但出現錯誤:
"error C2678: binary '>>' : no operator found which takes a left-hand operand of
type 'std::ifstream' (or there is no acceptable conversion)"
我搜索並發現僅建議嘗試包含更多標頭的建議,並嘗試包括: string, fstream, iostream, istream, vector
。
我可以使用fstream.get(),但是我正在嘗試獲取以空格分隔的字符串。 (我的文件格式是這樣的行: "String1 = String2"
)
這是我的代碼:
template <typename OutType>
OutType Read(std::ifstream& in)
{
OutType out;
in >> out;
return out;
}
任何建議都非常感謝! 謝謝!
(PS不確定對編譯器考慮是否重要,但我使用的是Visual Studio2013。)
問題是您的OutType
(您尚未顯示給我們的)沒有operator>>(istream&, OutType&)
。 您需要為每個可能的OutType
定義一個。
您期望>>
運算符如何知道OutType
? 它可以理解int
, char
等原語,但是如果要將OutType提供給<<,則應重載運算符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.