-
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?
-
DECLARE_SERIAL(Menu);
maybe?
-
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 ==========
-
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'
-
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...
-
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?
-
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 :(
-
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?
-
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?
-
Create cannot be virtual since you don't have an object to call it on.
I recommend Boost.Serialization for non-MFC serialization.
-
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.
-
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.