繁体   English   中英

在ifstream中使用运算符'>>'的编译器错误

[英]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 它可以理解intchar等原语,但是如果要将OutType提供给<<,则应重载运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM