tags:

views:

36

answers:

2

Hi need to copy the written objects in a file to copy to a array But following code gives me a Error

   T Obj
   T arr[20];
   while(file.read((char*)&Obj,sizeof(Obj))){
          int i=0;
             i++

            arr[i]==Obj;
            }

Error C2678: binary '==' : no operator found which takes a left-hand operand of type

+3  A: 

Well, firstly, the operator == is used for comparison, not assignment. For assignment, you want a single =. Secondly, your code is not portable, and possibly broken, because the way your object is stored on disk as a sequence of bytes is not necessarily the same way that it is stored in memory as a T object. This is because different computers/platforms/compilers represent binary data in different ways. Plus, as Vlad mentions in the comment below, if instances of T contain internal pointers, (like std::string), then your program will simply fall apart.

You should probably look into a serialization library, or at least use C++ iostreams to serialize your object into a text format, then use an istream_iterator to read them from disk.

Charles Salvia
... and if the object contains pointers, this kind of deserialization is plainly wrong.
Vlad
A: 

In C++ '==' is the equality operator, if you want to assign the object to the array 'arr' you should use a single '='. Also reading objects directly from a binary file is "questionable". You should use "stream operators" instead (Google it).

S.C. Madsen