Thread: Serializing a class

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    31

    Red face Serializing a class

    I'm very frustrated at this right now. I can't seem to figure it out.

    I created my own class called "CMais" and it used a generic class and the base class of the CObjectArray. I added 10 member variables and am trying to now serialize the class. When I go to add the member function, I type the function as "Serialize(CArchive &ar)" and the access as public and the virtual box is checked. When I click the "OK" button, it comes up with an error that "Serialize(CArchive &ar) is not a valid function in C++". What does this mean and how can I fix it?

    Thanks for all your help.

    I have attached a copy of my Mais.cpp file for you to look at for a deeper involved look.
    "Some succeed because they are destined to, others succeed because they are determined to."
    ~Anonymous

    "A shorn scrotum is quite breathtaking, I suggest you try it."
    ~Dr. Evil

    EMT/Firefighter
    Eagle Scout

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    To "serialize" a class, you would normally overload the stream insertion/extraction operators, (<< and >>), search google, or this board.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    serialization

    to implement serialization your object must be derive from CObject

    [CODE]

    //in h file



    class CMyclass : CObject
    {
    DECLARE_SERIAL(CMyclass)
    ...
    void Serialize( CArchive& ar);
    int m_1;
    int m_2;
    }

    /////////////////////////////////////////////
    //in cpp file


    IMPLEMENT_SERIAL( CMyclass, CObject, 0)

    void CMyclass::Serialize( CArchive& ar )
    {
    if( ar.IsStoring( ar ) )
    ar << m_1 << m_2 ;
    }


    /////in the foreing class

    class SomeForeignClass{


    CMyclass a;
    CFile myArFile;

    }


    void SomeForeignClass::OnLoad()
    {



    if( myArFile.Open(strFileName, CFile::modeRead))
    {
    CArchive ar(&myArFile, CArchive::load);
    a.Serialize( ar )l
    myArFile.Close();
    }

    }

    void SomeForeignClass::OnSave()
    {
    if( myArFile.Open( strFileName, CFile::modeCreate | CFile::modeWrite))
    {
    CArchive ar(&myArFile, CArchive::store)
    a.Serialize( ar );
    ar.Close();
    myArFile.Close();
    }

    }

    [CODE]

    this is somewhat of a sample similar to the one found in "Introduction to MFC programming with Visual C++" by Richard M. Jones....
    zMan

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    I have a similar question.. what if your class is Cwnd which is derived from CObject?

    because i'm doing what you just showed on top.. but with Cwnd.. and well it ain't working..

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    nevermind, i got it.. apprently you have to have a default constructor..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM