Object serialization of nested class

hello,
   I have a class X with a nested class Y. And I want to make class Y serializable. My code is
like this:

   class X : public CObject{
         class Y : public CObject{
                 DECLARE_SERIAL(Y)
         }
         DECLARE_SERIAL(X)
    }
    IMPLEMENT_SERIAL(X, CObject, 1)
    IMPLEMENT_SERIAL(X::Y, CObject, 1)

But error occurs during compiling:
error C2039: 'classX' : is not a member of 'Y'

Why?
syyung6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
Just a guess, but try
                     DECLARE_SERIAL(X::Y)

like
 class X : public CObject{
             class Y : public CObject{
                     DECLARE_SERIAL(X::Y)
             }
             DECLARE_SERIAL(X)
        }
        IMPLEMENT_SERIAL(X, CObject, 1)
        IMPLEMENT_SERIAL(X::Y, CObject, 1)
0
nietodCommented:
Do you know for a fact that an MFC class can be declared within another class?  It might not be possible.  Although C++ allows it, it doesn't necessarily mean that MFC's macros are able to handle it.
0
snemanovCommented:
Hello syyung6,
There are some syntax typo (see differences):
   class X : public CObject{
   public:
         class Y : public CObject{
                 DECLARE_SERIAL(Y)
         };
         DECLARE_SERIAL(X)
    };
    IMPLEMENT_SERIAL(X, CObject, 1)
    IMPLEMENT_SERIAL(X::Y, CObject, 1)

The main problem of this code, MFC is not supported nesated classes. It's following from implementation _IMPLEMENT_RUNTIMECLASS and IMPLEMENT_SERIAL macros.

After long experience and tortures to use nested classes mechanism, I think it's silly idea.
Personally, I find no reasons to use in nested classes.

Best wishes,
               Serge
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
issamwdCommented:
your code was:

   class X : public CObject{
         class Y : public CObject{
                 DECLARE_SERIAL(Y)
         }
         DECLARE_SERIAL(X)
    }
    IMPLEMENT_SERIAL(X, CObject, 1)
    IMPLEMENT_SERIAL(X::Y, CObject, 1)

             It is not accurate to define your classes like this you must define each class alone but,
you can derive one from the other but, I'll help you with your work.

   You can use another function which is SerializeClass() ; look carefully to the example at
the end of the page.
I'll try to give you the general form first,
the features of this member function are:

-CArchive::SerializeClass

void SerializeClass( const CRuntimeClass* pRuntimeClass );

-Parameters

pRuntimeClass   A pointer to a run-time class object for the base class.

-Remarks

Call this member function when you want to store and load the version information of a base
 class. SerializeClass reads or writes the reference to a class to the CArchive object, depending on the direction of the
CArchive. Use SerializeClass in place of ReadClass and WriteClass as a convenient way to
serialize base-class objects; SerializeClass requires less code and fewer parameters.

Like ReadClass, SerializeClass verifies that the archived class information is compatible with
your runtime class. If it is not compatible, SerializeClass will throw a CArchiveException.
Your runtime class must use DECLARE_SERIAL and IMPLEMENT_SERIAL;
otherwise, SerializeClass will throw a CNotSupportedException.


Use the RUNTIME_CLASS macro to retrieve the value for the pRuntimeClass parameter.
The base class must have used the IMPLEMENT_SERIAL macro.







I'll give you now a general example:
Example

class CBaseClass : public CObject { ... };
class CDerivedClass : public CBaseClass { ... };
void CDerivedClass::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        //normal code for storing contents
        //of this object
    }
    else
    {
        //normal code for reading contents
        //of this object
    }

    //allow the base class to serialize along
    //with its version information
   
 ar.SerializeClass(RUNTIME_CLASS(CBaseClass));
    CBaseClass::Serialize(ar);
}

      So now  you can replace your code with the above code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.