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
Solved

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

Posted on 2001-06-18
9
876 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 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Looking for a specific application/software 2 114
sameEnds challenge 3 180
post4 challenge 28 104
MaxSpan challenge 9 106
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

837 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