Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2001-06-18
9
Medium Priority
?
905 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Industry Leaders: 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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

609 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