2015-01-13 46 views
0

我试图使用自定义模板IO,我得到一个错误编译器错误使用操作符“>>”与ifstream的

"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; 
} 

任何建议都非常感谢!谢谢!

(附注:不知道它是否会为编译器的考虑无所谓,但我使用Visual Studio 2013年)

+1

什么是OutType当您尝试调用它时? – immibis

+0

大概应该阅读[这个问题](http://stackoverflow.com/questions/4421706/operator-overloading)。 – WhozCraig

回答

2

的问题是你OutType(你还没有告诉我们)没有operator>>(istream&, OutType&)。您需要为每个可能的OutType定义一个。

0

你如何期待OutType>>运算符所知?它理解像int,char等原始类型,但是如果要使OutType可用于< <,则应该重载操作符。

+0

@MattMcNabb更新了我的答案。 –