Solved

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

Posted on 2001-06-18
9
882 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 31

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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