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. Cannot initialize array of classes except as a local variable.

Cannot initialize array of classes except as a local variable.

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
6 Posts 4 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav

    C D S 3 Replies Last reply
    0
    • V Vaclav_

      I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      if you can make trackSwitch static, you can do this:

      class COpenTrack
      {
      public:

      static CTrackSwitch _trackSwitch[10];

      COpenTrack(void)
      {
      }
      };

      // outside the class definition
      CTrackSwitch COpenTrack::_trackSwitch[] =
      {
      CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
      CTrackSwitch(4,6,10,6),// normal reverse, input , switch
      CTrackSwitch(1,3,9,3),// normal reverse, input , switch
      CTrackSwitch(5,7,11,5),// normal reverse, input , switch
      CTrackSwitch(8,10,12,2),// normal reverse, input , switch
      CTrackSwitch(9,11,13,1),// normal reverse, input , switch
      CTrackSwitch(12,13,14,0)// normal reverse, input , switch
      };

      image processing toolkits | batch image processing

      V 1 Reply Last reply
      0
      • C Chris Losinger

        if you can make trackSwitch static, you can do this:

        class COpenTrack
        {
        public:

        static CTrackSwitch _trackSwitch[10];

        COpenTrack(void)
        {
        }
        };

        // outside the class definition
        CTrackSwitch COpenTrack::_trackSwitch[] =
        {
        CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
        CTrackSwitch(4,6,10,6),// normal reverse, input , switch
        CTrackSwitch(1,3,9,3),// normal reverse, input , switch
        CTrackSwitch(5,7,11,5),// normal reverse, input , switch
        CTrackSwitch(8,10,12,2),// normal reverse, input , switch
        CTrackSwitch(9,11,13,1),// normal reverse, input , switch
        CTrackSwitch(12,13,14,0)// normal reverse, input , switch
        };

        image processing toolkits | batch image processing

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        Boy that was fast. Thanks. I will try your way first. I think I'll punish myself and see if I can initialize it using constructor same way as I did the array. Cheers Vaclav

        1 Reply Last reply
        0
        • V Vaclav_

          I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          What about something like:

          COpenTrack::COpenTrack()
          {
          trackSwitch[0] = CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
          trackSwitch[1] = CTrackSwitch(4,6,10,6),// normal reverse, input , switch
          ...
          }

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          V 1 Reply Last reply
          0
          • D David Crow

            What about something like:

            COpenTrack::COpenTrack()
            {
            trackSwitch[0] = CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
            trackSwitch[1] = CTrackSwitch(4,6,10,6),// normal reverse, input , switch
            ...
            }

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            David, it works, but it is "not cool", just "plain" variable initialization. I did try to initialize the whole array in default constructor and it did not fly. I would probably get more descriptive error messages from some other IDE to figure it out. It should be same syntax I used for the CTrackSwitch, but I thing the array is messing it up. .... int iNormal; // passing thru int iReverse; // turning int iInputTrack; // int iSwitch; // may be usefull elesewhere // initialize using constructor CTrackSwitch(int iNormalInit,int iReverseInit,int iInputTrackInit, int iSwitchInit ) { iNormal = iNormalInit; iReverse = iReverseInit; iInputTrack = iInputTrackInit; iSwitch = iSwitchInit; } .... This is one attempt which actually compiled COpenTrack(class CTrackSwitch() ) { trackSwitch[10] = CTrackSwitch(); } but I could not get the constructor to work. Oh well, thanks for your help. Cheers Vaclav

            1 Reply Last reply
            0
            • V Vaclav_

              I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              In C++11 you should be able to use an initializer list just as you did in your local variable example. See http://stackoverflow.com/questions/161790/initialize-a-const-array-in-a-class-initializer-in-c[^] That said, of course your compiler needs to support it before you can use that syntax.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              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