Solved

problem on saving in mfc with Serialize(CArchive &ar)

Posted on 2001-06-18
9
864 Views
Last Modified: 2013-11-20
I created a base object called CBaseObject and it goes roughly like this:

class CBaseObject : public CObject  
{
     DECLARE_SERIAL(CBaseObject)

public:
     void Serialize(CArchive &ar);
     ...
     ...

}

The Serialize function is:
void CBaseObject::Serialize(CArchive &ar)
{
     CObject::Serialize(ar);
}

Then I created another class:

class CObjDoor : public CBaseObject  
{
public:
     void Serialize(CArchive &ar);
     ...
     ...

}

Which serialize function is:

void CObjDoor::Serialize(CArchive &ar)
{
     CBaseObject::Serialize(ar);
     if (ar.IsStoring())
          ar << nID << ptCenterPoint << fAngle <<
                 fFrameDepth << fFrameWidth << fTotalWidth <<
                fDoorThickness << fDoorOpenAngle << bFlipH << bFlipV;
     else
          ar >> nID >> ptCenterPoint >> fAngle >>
                 fFrameDepth >> fFrameWidth >> fTotalWidth >>
                fDoorThickness >> fDoorOpenAngle >> bFlipH >> bFlipV;
}

The class is used to store the door position, width, thickness, etc... but the problem is it doesnt save anything, when I open the file there is no doors...

It works fine with Wall Class that I previously created... it is directly inherit CObject as its base class.. so I guess the problem with the door is the inheritance?? pleaseee help meee....
0
Comment
Question by:techpage
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 3

Expert Comment

by:_mb_
ID: 6202866
Do you call these macro: IMPLEMENT_SERIAL (CObjDoor, CBaseObject, 1) ?
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 6203416
Hi techpage,

you'll need to declare 'Serial' as virtual at least in the base class, i.e.:

class CBaseObject : public CObject  
{
 DECLARE_SERIAL(CBaseObject)

 public:
  virtual void Serialize(CArchive &ar);
...
};

hope that helps,

ZOPPO
0
 
LVL 1

Expert Comment

by:nivel
ID: 6203501
virtual is good practice in cases like this, but as long as you don't do

ar << ((CBaseObject)obj);
instead of
ar << ((CObjDoor)obj);
it should work.

The IMPLEMENT_SERIAL, must be there though.
DECLARE_SERIAL(CObjDoor) is usually placed inside the class declaration, and IMPLEMENT_SERIAL(CObjDoor, CBaseObject, 1) in the definition file.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 6203565
well, I think if techpage would have forgotton one of these macros they compiler would
create a lot of errors...

Further, code like
ar << ((CBaseObject)obj);
won't be compiled at all until techpage implements an operator << and operator >> for use with CArchive...

so, he'll have to use something like CArchive::ReadObj() or CArchive::WriteObj() which get's as argument
a 'const CObject*', so calling this for CBaseObject* will call CBaseObject::Serialize(), but calling it with
a CObjDoor* will also call CBaseObject::Serialize(), since all classes derived from CBaseObject don't
have virtual Serialize() function implemented...

further, techpage, you should make sure to call base class's Serialize() function before serializing
the derived class...

ZOPPO
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:techpage
ID: 6204989
when I write:

IMPLEMENT_SERIAL(CObjDoor,CBaseObject,1)

the compiler showed error message:

error C2512: 'CObjDoor' : no appropriate default constructor available

what does that mean?
0
 
LVL 3

Author Comment

by:techpage
ID: 6204994
do you think that serialize can't work with a derived class?? it works okay with a class derived from CObject.

CBaseObject : CObject
CObjDoor : CBaseObject

so what's wrong with that... I guess it should work fine... but it's not !! why???
0
 
LVL 9

Accepted Solution

by:
Pacman earned 100 total points
ID: 6205167
> error C2512: 'CObjDoor' : no appropriate default constructor available

that means that your class "CObjDoor" has no default constructor. Add it to your class:

  CObjDoor();

I'm wondering how you could compile the code without using the IMPLEMENT_SERIAL() ...
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6205177
> you'll need to declare 'Serial' as virtual at least in the base class, i.e.:

Serialize is already virtual (in CObject).
Repeating the word "virtual" is good programming practice but doesn't change anything.
0
 

Expert Comment

by:tridot
ID: 7539979
IMPLEMENT_SERIAL(CWord,CObject,1)

error C2512: 'CArchive' : no appropriate default constructor available

It have default constructor "CWord();"
but
Why tell me 'CArchive' no appropriate default constructor available,what meanings?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

16 Experts available now in Live!

Get 1:1 Help Now