Solved

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

Posted on 2001-06-18
9
877 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 140
method notes when mouse over in eclipse 5 90
pre4 challenge 19 111
FizzBuzz challenge 9 85
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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