We help IT Professionals succeed at work.

Serialization Error

jaysonspeer
jaysonspeer asked
on
Medium Priority
307 Views
Last Modified: 2013-11-20
Dear Experts,

I am using VC++ 6/Win 98.

I have created an Abstract Class (CScreenObject) which has a number of pure virtual functions.

So far, I have created only one class (CNullObject) which utilises CScreenObject as it's base.

I can compile & link this project without any problems until I attempt to implement serialisation in the Base Class (CScreenObject).

Once I do this I get compiler errors (listed below) which indicate that the Pure Virtual functions have not been overriden in the Child Class.

Errors:

C:\ScreenSelect\ScreenObjects.cpp(7) : error C2259: 'CScreenObject' : cannot instantiate abstract class due to following members:
        c:\screenselect\screenobjects.h(47) : see declaration of 'CScreenObject'
C:\ScreenSelect\ScreenObjects.cpp(7) : warning C4259: 'int __thiscall CScreenObject::Highlight(class CDC *,const class CPoint &)' : pure virtual function was not defined
        c:\screenselect\screenobjects.h(58) : see declaration of 'Highlight'

The steps which I took to implement serialisation in the Base Class are as follows:

1. Created a Default Constructor.
2. Added the DECLARE_SERIAL macro in the Class Definition
3. Added the IMPLEMENT)_SERIAL macro in the Implementation File.
4. Create the function Serialize(CArchive& ar) in the Base Class (Virtual Function - not pure).

So my questio is what am I doing to create this error? and what do I do to get rid of it?

Thanks,

Jays.
Comment
Watch Question

Commented:
To implement serialization you need derive your class from CObject. CScreenObject could derive from CObject. Do not use the DECLARE_SERIAL and IMPLEMENT_SERIAL macros for the abstract base class. Use the macros for the derived class since only derived class objects will be serialized.
Hope this helps!

Author

Commented:
Hi vbk

Does this mean I can only use the Serialize function in the Child Classes?

Even though I have Child classes (which have different Draw Functions etc,..), all the Member Variables I want to serialise are actually members of the Base Class.

i.e:

class CBase
{
public:

protected:
CString strSerialVar1;
CString strSerialVar2;
etc,..

};

class CChild: public CBase
{

};

So, I don't want to have to create a Serialize function for each of the Child Classes when all the Serialisable Variables are in the Base Class.

p.s I'll gladly bump the points if this is a more difficult problem.

Thanks,

Jayson.
Commented:
You can override serialize for the base class(inherited from CObject) and serialize all your members in that. But you can use the macros DECLARE_SERIAL and IMPLEMENT_SERIAL for the derived classes and when instantiating the objects you can use  CBase *pBase = new CChild;

Commented:
hi there,

   plz. check this ----> all virtual functions must be override in chield class otherwise they r also virtual to child and u can't create objet of chield class.


regards,
himmya.

Author

Commented:
Hi himmya,

Isn't that only 'Pure Virtual' functions rather than just 'Virtual' functions?

Thanks,

Jayson.

Commented:
hi there,

yes .. yes ...  i just forgot to mention that...
thanks

regards,
himmya.

Author

Commented:
Thanks vbk,

Here is an A for your trouble! :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.