Lapys
Lapys

Reputation: 87

Compiler error for using operator '>>' with ifstream

I am trying to use a custom template for IO, and I getting an error :

"error C2678: binary '>>' : no operator found which takes a left-hand operand of 
type 'std::ifstream' (or there is no acceptable conversion)"

I have searched and found only suggestions to try including more headers, and have tried including: string, fstream, iostream, istream, vector.
I can use an fstream.get(), but I am trying to get space delimited strings. (The format of my file is lines like this: "String1 = String2")

Here is my code:

template <typename OutType>
OutType Read(std::ifstream& in)
{
    OutType out;
    in >> out;

    return out;
}

Any suggestions are very much appreciated! Thanks!

(P.S. Not sure if it will matter for compiler considerations, but I am using Visual Studio 2013.)

Upvotes: 0

Views: 115

Answers (2)

Pranit Kothari
Pranit Kothari

Reputation: 9841

How you are expecting OutType is known to >> operator? It understands primitives like int,char, etc., but if you want to make OutType available to << you should overload the operator.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

The problem is your OutType (which you have not shown us) has no operator>>(istream&, OutType&). You need to define one for every possible OutType.

Upvotes: 2

Related Questions