Saving CObArray objects containing CObArray objects
-
I would like to serialize the following CObArray class:
class CNode : public CSubNode { DECLARE_SERIAL (CNode) public: void Serialize(CArchive &ar); CNode(); virtual ~CNode(); CObArray * GetSubNodesP() {return &m_oaSubNodes;} void SetNodePath(CString inPath) {m_strNodDescr = inPath;} CString GetNodePath() {return m_strNodDescr;} private: int m_iCurPosition; CString m_strNodDescr; CObArray m_oaSubNodes; };
Here is the serialization function in the document class....void CFTreeBrowserDoc::Serialize(CArchive& ar) { //m_oaNodes::Serialize(ar); m_oaNodes.Serialize(ar); }
...and in CNode...void CNode::Serialize(CArchive &ar) { m_oaSubNodes.Serialize(ar); // Are we writing? if (ar.IsStoring()) // Write all of the variables, in order ar << m_iCurPosition << m_strNodDescr; // << m_oaSubNodes; else // Read all of the variables, in order ar >> m_iCurPosition >> m_strNodDescr; // >> m_oaSubNodes; }
...and so far (because I haven't got around to implementing what I want to do with it yet) in the CSubNode class...void CSubNode::Serialize(CArchive &ar) { // Call the ancestor function CObject::Serialize(ar); }
Now because I haven't yet got very far at all with this, I have only tried writing and reading values to and from 'm_strNodDescr' in the CNode objects. The program writes these values to a file which it saves. I know this has worked, because I can see the values when I open the file in a generic editor. However, when I load the file back into my application, the array of CNode objects, as far as I can tell, doesn't get re-populated. Your help and opinions here are much appreciated, Ben. -
I would like to serialize the following CObArray class:
class CNode : public CSubNode { DECLARE_SERIAL (CNode) public: void Serialize(CArchive &ar); CNode(); virtual ~CNode(); CObArray * GetSubNodesP() {return &m_oaSubNodes;} void SetNodePath(CString inPath) {m_strNodDescr = inPath;} CString GetNodePath() {return m_strNodDescr;} private: int m_iCurPosition; CString m_strNodDescr; CObArray m_oaSubNodes; };
Here is the serialization function in the document class....void CFTreeBrowserDoc::Serialize(CArchive& ar) { //m_oaNodes::Serialize(ar); m_oaNodes.Serialize(ar); }
...and in CNode...void CNode::Serialize(CArchive &ar) { m_oaSubNodes.Serialize(ar); // Are we writing? if (ar.IsStoring()) // Write all of the variables, in order ar << m_iCurPosition << m_strNodDescr; // << m_oaSubNodes; else // Read all of the variables, in order ar >> m_iCurPosition >> m_strNodDescr; // >> m_oaSubNodes; }
...and so far (because I haven't got around to implementing what I want to do with it yet) in the CSubNode class...void CSubNode::Serialize(CArchive &ar) { // Call the ancestor function CObject::Serialize(ar); }
Now because I haven't yet got very far at all with this, I have only tried writing and reading values to and from 'm_strNodDescr' in the CNode objects. The program writes these values to a file which it saves. I know this has worked, because I can see the values when I open the file in a generic editor. However, when I load the file back into my application, the array of CNode objects, as far as I can tell, doesn't get re-populated. Your help and opinions here are much appreciated, Ben.Ben Aldhouse wrote:
void CNode::Serialize(CArchive &ar) {
Did you forget to call base class member here ?
CObject::Serialize(ar)
in this case .Prasad Notifier using ATL | Operator new[],delete[][^]
-
Ben Aldhouse wrote:
void CNode::Serialize(CArchive &ar) {
Did you forget to call base class member here ?
CObject::Serialize(ar)
in this case .Prasad Notifier using ATL | Operator new[],delete[][^]
Thanks for your answer,I will go away and have a look...
-
Thanks for your answer,I will go away and have a look...
Ok , so I have checked. I had been calling the ancestor function in the CSubNode class, but not in the CNode class serialization function. Both serialization functions now call the ancestor function. The result is the same. I can save (at least, to some extent), but not reload the data back into the array.
-
I would like to serialize the following CObArray class:
class CNode : public CSubNode { DECLARE_SERIAL (CNode) public: void Serialize(CArchive &ar); CNode(); virtual ~CNode(); CObArray * GetSubNodesP() {return &m_oaSubNodes;} void SetNodePath(CString inPath) {m_strNodDescr = inPath;} CString GetNodePath() {return m_strNodDescr;} private: int m_iCurPosition; CString m_strNodDescr; CObArray m_oaSubNodes; };
Here is the serialization function in the document class....void CFTreeBrowserDoc::Serialize(CArchive& ar) { //m_oaNodes::Serialize(ar); m_oaNodes.Serialize(ar); }
...and in CNode...void CNode::Serialize(CArchive &ar) { m_oaSubNodes.Serialize(ar); // Are we writing? if (ar.IsStoring()) // Write all of the variables, in order ar << m_iCurPosition << m_strNodDescr; // << m_oaSubNodes; else // Read all of the variables, in order ar >> m_iCurPosition >> m_strNodDescr; // >> m_oaSubNodes; }
...and so far (because I haven't got around to implementing what I want to do with it yet) in the CSubNode class...void CSubNode::Serialize(CArchive &ar) { // Call the ancestor function CObject::Serialize(ar); }
Now because I haven't yet got very far at all with this, I have only tried writing and reading values to and from 'm_strNodDescr' in the CNode objects. The program writes these values to a file which it saves. I know this has worked, because I can see the values when I open the file in a generic editor. However, when I load the file back into my application, the array of CNode objects, as far as I can tell, doesn't get re-populated. Your help and opinions here are much appreciated, Ben.I've got this sorted out now. The full solution to the problem is written up here. Looking at the above message, it seems to me that, at the time, my mistake was in the CNode::Serialize function where I had simply tried to serialize the CObArray object (of sub nodes) with the insertion and extraction operators along with the other node object members. The way to get this to work, it turns out, is to not treat the collection of sub nodes like the other node object members but instead call its CObArray serialization function. Hence the node object serialization function looks like this:
void CNode::Serialize(CArchive &ar)
{
// Call the ancestor function
CSubNode::Serialize(ar);int iNoSubNodes = 0; // Are we writing? if (ar.IsStoring()) { // Get the number of sub nodes iNoSubNodes = m\_oaSubNodes.GetSize(); // Write variables in order ar << m\_iSubNodePosition << m\_strString << iNoSubNodes; } else // Read variables in order ar >> m\_iSubNodePosition >> m\_strString >> iNoSubNodes; // Serialize the array of sub nodes m\_oaSubNodes.Serialize(ar);
}