Solved

Serialization of non-MFC

Posted on 1998-08-04
9
402 Views
Last Modified: 2013-11-19
In MS Scibble example one has the following code:
void CScribbleDoc::Serialize(CArchive& ar)
{
      if (ar.IsStoring())
            ar << m_sizeDoc;
      else
            ar >> m_sizeDoc;
      m_strokeList.Serialize(ar);
}
m_strokeList is a MFC. It has a serialze function.
What does one do if your want to serialize a non-MFC.
Current
"MyClass.h"
#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
private:
      int m_maj;
      float m_min;
public:
      MyClass();
};
#endif
"MyClass.cpp"
#include "MyClass.h"

MyClass::MyClass()
{
      m_maj = 3;
      m_min = 3.3;
}
"Serialize_MyClass.h"
#ifndef SERIALIZE_MYCLASS_H
#define SERIALIZE_MYCLASS_H

class CArchive;
class MyClass;

CArchive &operator << (CArchive &ar, const MyClass *pMyClass);

#endif
"Serialize_MyClass.cpp"
#include <stdafx.h>
#include "Serialize_MyClass.h"
#include "MyClass.h"
CArchive &operator <<(CArchive& ar, const MyClass *pMyClass)
{
...
            return ar;
}
I then include Serialize_MyClass into ScribbleDoc.cpp file.
I try ar << m_MyClass; //member variable define as MyClass
I get the following error:
error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class CArchive' (or there is no acceptable conversion)
NO CODE is to be added to MyClass. Please show header files and cpp files and code. I believe I have to override operator << for CArchive to handle class MyClass. In the overridden function I have to ar << (simply data types).
Is this correct at all? Please also show if I am using classes by name only, what include files I need.
Help
0
Comment
Question by:Paullkha
9 Comments
 
LVL 2

Author Comment

by:Paullkha
ID: 1320238
Edited text of question
0
 
LVL 2

Author Comment

by:Paullkha
ID: 1320239
Edited text of question
0
 
LVL 2

Author Comment

by:Paullkha
ID: 1320240
Adjusted points to 80
0
 

Expert Comment

by:t004024
ID: 1320241
Look at the article in MFC help :

CObject Class topics,  in Adding Program Functionalities, Details

As far as I understood U need to have the class derived from CObject, only then U can add the serialization functionality and U don't seem to have done that.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:Paullkha
ID: 1320242
Mike Blaszczak, author of Professional MFC VC++5, states that there are 3 ways one can use for types not supported by CArchive, overload <<, develope a macro, or read or write the type. He provides no examples however.
0
 
LVL 1

Accepted Solution

by:
Arkadiy earned 80 total points
ID: 1320243
You say: "member variable define as MyClass". However, the operator you define
CArchive &operator << (CArchive &ar, const MyClass *pMyClass);
expects a pointer to MyClass. You have to change the operator header to
CArchive &operator << (CArchive &ar, const MyClass &pMyClass);

0
 

Expert Comment

by:glasgow
ID: 1320244

You can serialize any class using CArchive and the << operator.

When MFC serializes objects, it writes out the class of the
object it is serializing first, then the version of the object,
then it calls the objects serilize routine to actually write the
data. When it reads an object in, the class of the object is
read, and an object of this type is created.  the version is
read, and then the serialize routine of the object is called to
load the data into the object.

For any object to use CArchive, you need to set up the runtime
information for your class.  There are a  bunch of macros in
MFC to do this.  The ones that you need are DECLARE_SERIAL
and IMPLEMENT_SERIAL.   Your class would look like:

class CMyClass : public CObject {

  DECLARE_SERIAL(CMyClass)

public:
 ... rest of class
};


in cpp file:

//  class, parent_class, and version number of your object.
IMPELEMENT_SERIAL(CMyClass, CObject, 0x100);

void
CMyClass::Serialize( CArchive  &ar )
{
}


These macros will set up the runtime class information, and
overload all of the << and >> operators so that you can
serialize your objects just like any other MFC class.
This class will now work with the MFC colleciton classes.
So, if you have a CObArray of CMyClass structues, you can
serialize the entire class.

Michael


0
 
LVL 2

Author Comment

by:Paullkha
ID: 1320245
Arkadiy.
THANKS! There was a bug in the code, but why did it keep saying left hand side?. I actually did I quick proto to see if I could overload the operator<< the way I was. I did not make that mistake in the quick proto. Oh Well.
However, is this the proper way to accomplish what I want?
ar << m_MyClass will cause the overloaded function to perform the following tasks:
ar << simpletype1_in_MyClass
ar << simpletype2_in_MyClass
Also, should I make the overloaded function a friend of MyClass, accessing the private members directly? Or call functions to get the private data members? I believe the latter is more secure since overloaded operator<< and operator>> are in a seperate module.
0
 
LVL 1

Expert Comment

by:Arkadiy
ID: 1320246
Well, a better error message would be something like "There is no operator<<(CArchive&, MyClass&)". I can't tell why the compiler's author did it that way, but there probably is a reason.

Conserning the things you do in your operator... That's up to you, isn't it? I see three options: you can just call Write/Read on the entire instance (works if you have no virtual functions, pointers, subclasses etc.), or you can do what you do now.

Calling functions to get private members is fine as long as you have functions to set them as well :). And if the functions are just asignement operators, consider if you really need them.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now