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. How to create and array of structs that have their own arrays? [modified]

How to create and array of structs that have their own arrays? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelptutorialquestion
10 Posts 3 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.
  • E Offline
    E Offline
    Electronic75
    wrote on last edited by
    #1

    Hello :) , is there anyway that I can hold an array of structs that have their own arrays other than creating a new class

    somewhere in the typedef.h ...
    typedef struct mystruct
    {
    CStringArray aName1;
    CStringArray aName2;
    CArray aVal1;
    CArray aVal2;
    }MY_STRUCT;

    somewhere in the header of my MyDoc.h...
    #include "typedef.h"

    CArray m_MyStructs;
    somewhere in the header of my MyDoc.cpp...

    MY_STRUCT a;
    ...
    m_MyStruct.Add(a);

    This code returns error that "=" operator is private and can't be accessed! Thanks :) PS-Oh I got that I can'tuse >< around my type specifier because it will be assumed as a tag so I use [] instead :) somewhere in the typedef.h ... typedef struct mystruct { CStringArray aName1; CStringArray aName2; CArray [int] aVal1; CArray [int] aVal2; }MY_STRUCT;

    C C 2 Replies Last reply
    0
    • E Electronic75

      Hello :) , is there anyway that I can hold an array of structs that have their own arrays other than creating a new class

      somewhere in the typedef.h ...
      typedef struct mystruct
      {
      CStringArray aName1;
      CStringArray aName2;
      CArray aVal1;
      CArray aVal2;
      }MY_STRUCT;

      somewhere in the header of my MyDoc.h...
      #include "typedef.h"

      CArray m_MyStructs;
      somewhere in the header of my MyDoc.cpp...

      MY_STRUCT a;
      ...
      m_MyStruct.Add(a);

      This code returns error that "=" operator is private and can't be accessed! Thanks :) PS-Oh I got that I can'tuse >< around my type specifier because it will be assumed as a tag so I use [] instead :) somewhere in the typedef.h ... typedef struct mystruct { CStringArray aName1; CStringArray aName2; CArray [int] aVal1; CArray [int] aVal2; }MY_STRUCT;

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Could you report the exact (complete) error message?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      E 1 Reply Last reply
      0
      • C CPallini

        Could you report the exact (complete) error message?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        E Offline
        E Offline
        Electronic75
        wrote on last edited by
        #3

        Hi :) , Sure, Error 36 error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxcoll.h 593 Error 37 error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxtempl.h 272 these errors do not appear when I remove m_Mystruct.Add(a) As I noted in the stuct definition I have both CArray and CStringArray thanks :)

        C 1 Reply Last reply
        0
        • E Electronic75

          Hi :) , Sure, Error 36 error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxcoll.h 593 Error 37 error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxtempl.h 272 these errors do not appear when I remove m_Mystruct.Add(a) As I noted in the stuct definition I have both CArray and CStringArray thanks :)

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Could you also reformat your code properly? For instance CArray is a template container, but I don't see any argument between < >. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          E 1 Reply Last reply
          0
          • E Electronic75

            Hello :) , is there anyway that I can hold an array of structs that have their own arrays other than creating a new class

            somewhere in the typedef.h ...
            typedef struct mystruct
            {
            CStringArray aName1;
            CStringArray aName2;
            CArray aVal1;
            CArray aVal2;
            }MY_STRUCT;

            somewhere in the header of my MyDoc.h...
            #include "typedef.h"

            CArray m_MyStructs;
            somewhere in the header of my MyDoc.cpp...

            MY_STRUCT a;
            ...
            m_MyStruct.Add(a);

            This code returns error that "=" operator is private and can't be accessed! Thanks :) PS-Oh I got that I can'tuse >< around my type specifier because it will be assumed as a tag so I use [] instead :) somewhere in the typedef.h ... typedef struct mystruct { CStringArray aName1; CStringArray aName2; CArray [int] aVal1; CArray [int] aVal2; }MY_STRUCT;

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            I guess it is because CArray and CStringArray don't have a copy constructor. So, the compiler will assign a default copy constructor and assignment operator to your structure which 'uses' the copy ctor and assignment operator of the CArray and CStringArray, which are private. One way to fix this is to provide your own copy constructor and assignment operators for the structure. In those, copy the contents of the arrays using a loop (instead of a direct assignment).

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            E 1 Reply Last reply
            0
            • C CPallini

              Could you also reformat your code properly? For instance CArray is a template container, but I don't see any argument between < >. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              E Offline
              E Offline
              Electronic75
              wrote on last edited by
              #6

              done :)

              C 1 Reply Last reply
              0
              • C Cedric Moonen

                I guess it is because CArray and CStringArray don't have a copy constructor. So, the compiler will assign a default copy constructor and assignment operator to your structure which 'uses' the copy ctor and assignment operator of the CArray and CStringArray, which are private. One way to fix this is to provide your own copy constructor and assignment operators for the structure. In those, copy the contents of the arrays using a loop (instead of a direct assignment).

                Cédric Moonen Software developer
                Charting control [v1.5] OpenGL game tutorial in C++

                E Offline
                E Offline
                Electronic75
                wrote on last edited by
                #7

                Hi! :) I guess you are right! you mean I have to derive my own CArray class? isn't another way I mean another array class that can do the trick! thanks

                C 1 Reply Last reply
                0
                • E Electronic75

                  done :)

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Probably Cédric's guess [^] is right. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • E Electronic75

                    Hi! :) I guess you are right! you mean I have to derive my own CArray class? isn't another way I mean another array class that can do the trick! thanks

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #9

                    Electronic75 wrote:

                    you mean I have to derive my own CArray class

                    No, I mean you have to provide a =operator and a copy constructor for your structure. Inside those, instead of simply assigning the arrays, like this:

                    mystruct::myStruct(const mystruct& copy)
                    {
                    array1 = copy.array1;
                    }

                    you will need to copy the content of the array by iterating over it.

                    Electronic75 wrote:

                    isn't another way I mean another array class that can do the trick!

                    std::string or std::list maybe ? But you have to be carefull of what is inside those arrays. If you store pointers, you will have a copy of the original pointer pointing at the same memory location, so you should only delete one of them (so, it's a bit dangerous).

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    E 1 Reply Last reply
                    0
                    • C Cedric Moonen

                      Electronic75 wrote:

                      you mean I have to derive my own CArray class

                      No, I mean you have to provide a =operator and a copy constructor for your structure. Inside those, instead of simply assigning the arrays, like this:

                      mystruct::myStruct(const mystruct& copy)
                      {
                      array1 = copy.array1;
                      }

                      you will need to copy the content of the array by iterating over it.

                      Electronic75 wrote:

                      isn't another way I mean another array class that can do the trick!

                      std::string or std::list maybe ? But you have to be carefull of what is inside those arrays. If you store pointers, you will have a copy of the original pointer pointing at the same memory location, so you should only delete one of them (so, it's a bit dangerous).

                      Cédric Moonen Software developer
                      Charting control [v1.5] OpenGL game tutorial in C++

                      E Offline
                      E Offline
                      Electronic75
                      wrote on last edited by
                      #10

                      Thanks a lot Cedric, It worked pretty much OK:cool::rose:

                      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