Serialization yay!

This is a discussion on Serialization yay! within the C++ Programming forums, part of the General Programming Boards category; Code: #ifndef MENU_H #define MENU_H #include "Object.h" #include <string> #include <list> class Menu : public CObject { public: Menu(){}; Menu(std::string ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Serialization yay!

    Code:
    #ifndef MENU_H
    #define MENU_H
    
    #include "Object.h"
    #include <string>
    #include <list>
    
    class Menu : public CObject
    {
    public:
    	Menu(){};
    	Menu(std::string mSelection) 
    	{
    		mSelection = Selection;
    	}
    
    	virtual ~Menu() { Destroy(); }
    
    	// deletes self
    	void Release() { delete this; }
    
    	// call update on all children
    	virtual void Update()
    	{
    		for( std::list<Menu*>::iterator i = MenuChoices.begin();
    			i != MenuChoices.end(); i++ )
    		{
    			(*i)->Update();
    		}
    	}
    
    	// recursively destroy all children and self
    	void Destroy()
    	{
    		for( std::list<Menu*>::iterator i = MenuChoices.begin();
    			i != MenuChoices.end(); i++ )
    		(*i)->Release();
      
    		MenuChoices.clear();
    	}
    
    	// add a child
    	void MenuSelection( Menu* MenuSelection )
    	{
    		MenuSelection->SetRootMenu(this);
    	    MenuChoices.push_back(MenuSelection);
    	}
    
    	// Set the parent of the child
    	void SetRootMenu(Menu* Root)
    	{
    		RootMenu = Root;
    	}
    
    protected:
    	DECLARE_SERIAL(Menu)
    	// list of children
    	std::list<Menu*> MenuChoices;
    	// pointer to parent
    	Menu * RootMenu;
    
    	std::string Selection;
    	void Serialize( CArchive& archive );
    
    };
    #endif
    Okay so I'm getting these errors because of the serialization stuff:
    Code:
    ------ Build started: Project: TextGame, Configuration: Debug Win32 ------
    Compiling...
    Main.cpp
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(60) : error C2143: syntax error : missing ';' before 'std::list<_Ty>'
            with
            [
                _Ty=Menu *
            ]
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(60) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(60) : warning C4183: 'DECLARE_SERIAL': missing return type; assumed to be a member function returning 'int'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(65) : error C2061: syntax error : identifier 'CArchive'
    Build log was saved at "file://c:\Documents and Settings\Home\My Documents\Visual Studio 2005\Projects\TextGame\TextGame\Debug\BuildLog.htm"
    TextGame - 3 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    According to the MFC website this should work without a hitch, the only thing I'm doing differently is making a vector available for serialization. Any help?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    DECLARE_SERIAL(Menu);
    maybe?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Cool I didn't know it needed a ";" ending it because the MFC sample code didn't include it, but I guess I should have known...

    This leaves two errors:
    Code:
    ------ Build started: Project: TextGame, Configuration: Debug Win32 ------
    Compiling...
    Main.cpp
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(57) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(57) : warning C4183: 'DECLARE_SERIAL': missing return type; assumed to be a member function returning 'int'
    c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\menu_object.h(65) : error C2061: syntax error : identifier 'CArchive'
    Build log was saved at "file://c:\Documents and Settings\Home\My Documents\Visual Studio 2005\Projects\TextGame\TextGame\Debug\BuildLog.htm"
    TextGame - 2 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    You forgot to include boost libraries (if you use them for serailization) or you use them in a wrong way.

    error C2061: syntax error : identifier 'CArchive'

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm using MFC, but I realized I needed afx.h included, I've done that, now I have a weird linker error...
    Code:
    ------ Build started: Project: TextGame, Configuration: Debug Win32 ------
    Compiling...
    Main.cpp
    Linking...
    LINK : fatal error LNK1104: cannot open file 'uafxcwd.lib'
    Build log was saved at "file://c:\Documents and Settings\Home\My Documents\Visual Studio 2005\Projects\TextGame\TextGame\Debug\BuildLog.htm"
    TextGame - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    This also forced me to change my CObject class to Object : public CObject

    Is this correct?

    I've learned that uafxcwd.lib is not included in my psdk because I'm using vs2005 EE, I've googled and they say i need to upgrade to standard edition to use this library, freakin lame...
    Last edited by Shamino; 06-10-2007 at 06:59 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It doesn't find a file from MFC. Do you have at least the Standard edition and is it correctly set up? Did you create your project as an MFC project? (Otherwise, you might have to adjust library search paths.)

    It's correct that you must change CObject to Object, because MFC doesn't use namespaces and has its own CObject class. But are you sure you need your own?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm sure I need my own because I need a virtual create, access, read file and save file and delete method for each individual object.

    But I'm running in express edition and google says that is probably my problem, I have to buy standard edition?!

    I have verified that the file does not exist on my computer
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The Express edition doesn't come with MFC, indeed. I'm surprised it can even find afx.h.

    Virtual read file and save file are what serialization is about; you don't need them in a base class. Create, too, is supplied by CObject and the DECARE_DYNCREATE macro (included in DECLARE_SERIALIZE). Delete - what for? Just use the delete operator. And what do you mean by access?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Create needs to be virtual as not all objects are created in the same way. What should I do for serialization if I can't use MFC?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Create cannot be virtual since you don't have an object to call it on.

    I recommend Boost.Serialization for non-MFC serialization.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Shamino you cannot follow me example from game programming forum inside of MFC. First MFC already has a base class CObject so you will have a naming conflict right off that bat. Second MFC already provides base serialization support for all MFC objects so you don't have to.

    The post in game programming forum was for serializing your own objects inside of your own system.

    For MFC it's actually quite simple.

    • First create a CFile object
    • Second create a CArchive object and pass the CFile object to it
    • The stream to use for serialization is the CArchive object.


    I'll show you an example from my tile editor.

    Code:
    void CTileEditorDoc::OnFileSaveAs()
    {
      //Save as dialog
      CFileDialog *dlg=new CFileDialog(FALSE,
                                       "*.prj",
                                       NULL,
                                       OFN_OVERWRITEPROMPT | OFN_CREATEPROMPT,
                                       "Project files (.prj)|*.prj|");
     
      INT_PTR result=dlg->DoModal();
      
      if (result==IDOK)
      {
        //Pointer to main frame
        CMainFrame *pFrame=(CMainFrame *)AfxGetMainWnd();
        
        //CFile object
        CFile file;
        file.Open(dlg->GetPathName(),CFile::modeCreate | CFile::modeWrite);
        
        //CArchive object
        CArchive ar(&file,CArchive::store);
        
        pFrame->Serialize(ar);
       
        ar.Close();
        file.Close();
        
        AfxMessageBox("Project saved",MB_ICONINFORMATION);
        
      }
      delete dlg;
      
      SetModifiedFlag(FALSE);
     
    }
    
    ...
    ...
    void CMainFrame::Serialize(CArchive& ar)
    {
      if (ar.IsStoring())
      {	
        // storing code
        //Write document to disk (map manager for view only)
        m_pDoc->Serialize(ar);
        m_pView->Serialize(ar);
        m_pTileSheet->Serialize(ar);
         
      }
      else
      {	
        // loading code
        m_pDoc->Serialize(ar);
        m_pView->Serialize(ar);
        
        m_pTileSheet=new CTileSheet("Tile palette",this);
        m_pTileSheet->Serialize(ar);
         
      }
    }
    As you can see the serialization works like a huge chain. Once the save as command is clicked on by the user, it brings up a dialog for the user to interact with. Once the user is done with the dialog the serialization begins. My main frame serialization function passes the CArchive object to other serialize functions so that in the end every object is serialized. Some of my objects do not use MFC serialization because while it's good at doing some objects there are times where you need your own serialization functions.

    If you look closely at CArchive you will see that it is a very simple class to use. The hardest part is realizing that CFile is actually the file object and not CArchive.

    And no you cannot use MFC with Express Edition. Trying to will be a huge headache since Express lacks all of the important GUI design stuff for MFC. If you get an edition that does support MFC you will find that at first it will be confusing......but quickly becomes your best friend.

    Some here loathe MFC. I, for one, enjoy it.
    Last edited by VirtualAce; 06-10-2007 at 02:08 PM.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I guess I'll use boost for serialization, smart and shared pointers have already made my code hundreds of times better...

    So I can't name my main object class CObject if I use MFC? I just won't use MFC then I suppose...

    I can't anyways...

    So I guess I'll just make it CObject and use boost.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serialization problem
    By noobcpp in forum C++ Programming
    Replies: 17
    Last Post: 07-15-2008, 09:01 PM
  2. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 08:17 PM
  3. Replies: 3
    Last Post: 06-12-2007, 12:21 PM
  4. serialization and object types
    By Joe Monti in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 11:17 AM
  5. Serialization & Reading/Write Multiple Lines :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-10-2002, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21