Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Saving CObArray objects containing CObArray objects

Saving CObArray objects containing CObArray objects

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresjsonhelpquestiondiscussion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Ben Aldhouse
    wrote on last edited by
    #1

    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.

    P B 2 Replies Last reply
    0
    • B Ben Aldhouse

      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.

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      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[][^]

      B 1 Reply Last reply
      0
      • P prasad_som

        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[][^]

        B Offline
        B Offline
        Ben Aldhouse
        wrote on last edited by
        #3

        Thanks for your answer,I will go away and have a look...

        B 1 Reply Last reply
        0
        • B Ben Aldhouse

          Thanks for your answer,I will go away and have a look...

          B Offline
          B Offline
          Ben Aldhouse
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B Ben Aldhouse

            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.

            B Offline
            B Offline
            Ben Aldhouse
            wrote on last edited by
            #5

            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);
            

            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups