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. Array of objects?

Array of objects?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
11 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.
  • C clintsinger

    Is it possible to create an array of objects or an array of pointers to objects. I think the later is what I am really after. I have tried doing such a beast and all I get is an error.class Bar { Bar(CString strName, CString strAddress) } class Foo { protected: Bar* m_poBars[16]; public: Foo(); Create(INT nIndex); } ////////// Implementation Foo::Foo() { for (INT i; i < 16; i++) { m_poBars[i] = NULL; // This is not liked at all } } // Then I would like to initialize an arbitrary location with the Bar class Foo::Create(INT nIndex) { m_poBars[nIndex] = new Bar("My Place", "123 Road"); }

    The above code doesn't seem to be too happy X| and I was wondering what I am doing wrong. Cheers, Clint Singer

    J Offline
    J Offline
    Joaquin M Lopez Munoz
    wrote on last edited by
    #2

    I think everything is perfect except the following line:

    for (INT i; i < 16; i++)

    You forgot to initialize i:

    for (INT i=0; i < 16; i++)

    Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

    C 1 Reply Last reply
    0
    • C clintsinger

      Is it possible to create an array of objects or an array of pointers to objects. I think the later is what I am really after. I have tried doing such a beast and all I get is an error.class Bar { Bar(CString strName, CString strAddress) } class Foo { protected: Bar* m_poBars[16]; public: Foo(); Create(INT nIndex); } ////////// Implementation Foo::Foo() { for (INT i; i < 16; i++) { m_poBars[i] = NULL; // This is not liked at all } } // Then I would like to initialize an arbitrary location with the Bar class Foo::Create(INT nIndex) { m_poBars[nIndex] = new Bar("My Place", "123 Road"); }

      The above code doesn't seem to be too happy X| and I was wondering what I am doing wrong. Cheers, Clint Singer

      H Offline
      H Offline
      HomeNuke
      wrote on last edited by
      #3

      Like Joaquin said it looks good except for the initialazation part of 'i'. However, if that does not resolve your problem what is the specific error message you are getting? That would help diagnose the problem. PLUS are you sure that is where the error is generated? Your example class BAR is defined such as:

      class Bar
      {
      Bar(int strName, int strAddress);
      };

      The problem with this is that Bar is now a private Constructor by default. You would have problems initializing this in your main function when it calls FOO create unless this is declared as a friend of the class, but that wasn't appearent in your example. Plus you may want to add to the end of your constructor { } so it would be an inline function or define it somewhere else or you may get a linker error. Hope this helps out...I don't even know what I'm talking about right now...need more coffee :| HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

      C 1 Reply Last reply
      0
      • H HomeNuke

        Like Joaquin said it looks good except for the initialazation part of 'i'. However, if that does not resolve your problem what is the specific error message you are getting? That would help diagnose the problem. PLUS are you sure that is where the error is generated? Your example class BAR is defined such as:

        class Bar
        {
        Bar(int strName, int strAddress);
        };

        The problem with this is that Bar is now a private Constructor by default. You would have problems initializing this in your main function when it calls FOO create unless this is declared as a friend of the class, but that wasn't appearent in your example. Plus you may want to add to the end of your constructor { } so it would be an inline function or define it somewhere else or you may get a linker error. Hope this helps out...I don't even know what I'm talking about right now...need more coffee :| HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

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

        Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).

        H J J 3 Replies Last reply
        0
        • C clintsinger

          Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).

          H Offline
          H Offline
          HomeNuke
          wrote on last edited by
          #5

          So the error you are getting is from the client side? Oh OK I'm thinking from a straight C++ side application :) Let me review this some more...sorry for not understanding the question in the first place. HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com

          1 Reply Last reply
          0
          • C clintsinger

            Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #6

            I'd try to reproduce the error in a VC++-only application, without all the ActiveX mess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            C 1 Reply Last reply
            0
            • J Joaquin M Lopez Munoz

              I'd try to reproduce the error in a VC++-only application, without all the ActiveX mess. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              C Offline
              C Offline
              clintsinger
              wrote on last edited by
              #7

              I tried it with a MFC exe dialog and it seems to work just as I had expected it too. I guess I will have to do some more trial and error to find the problem. Cheers, Clint Singer

              1 Reply Last reply
              0
              • J Joaquin M Lopez Munoz

                I think everything is perfect except the following line:

                for (INT i; i < 16; i++)

                You forgot to initialize i:

                for (INT i=0; i < 16; i++)

                Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

                I can't believe it. The INT i... Well it was the problem. I made the mistake in my posting and in my code. What is worse, the posting was totally made up to simplify the problem. I don't know how many times I looked at my real code and didn't notice that I wasn't initalizing i to Zero. Boy, I feel silly :laugh: Cheers, Clint Singer

                J 1 Reply Last reply
                0
                • C clintsinger

                  I can't believe it. The INT i... Well it was the problem. I made the mistake in my posting and in my code. What is worse, the posting was totally made up to simplify the problem. I don't know how many times I looked at my real code and didn't notice that I wasn't initalizing i to Zero. Boy, I feel silly :laugh: Cheers, Clint Singer

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #9

                  Didn't you get a warning? The compiled should have issued one on this. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  1 Reply Last reply
                  0
                  • C clintsinger

                    Some mistakes on my part. The Bar() constructor is infact public: and INT i is indeed INT i = 0; They are just wrong on my posting, sorry. I am still getting the error. To add more to the story I am making a simple ActiveX control and running it in visual basic. The errors I get are as such when I try to do m_poBars[i] = NULL; First-chance exception in VB6.EXE (DVMS CLIENT LIB.DLL): 0xC0000005: Access Violation. First-chance exception in VB6.EXE (KERNEL32.DLL): 0x80010105: (no name).

                    J Offline
                    J Offline
                    Jon Hulatt
                    wrote on last edited by
                    #10

                    If you're runnign in ActiveX from VB, then how do you know what line is causing the problem???? Sorry to dissapoint you all with my lack of a witty or poignant signature.

                    J 1 Reply Last reply
                    0
                    • J Jon Hulatt

                      If you're runnign in ActiveX from VB, then how do you know what line is causing the problem???? Sorry to dissapoint you all with my lack of a witty or poignant signature.

                      J Offline
                      J Offline
                      Jon Hulatt
                      wrote on last edited by
                      #11

                      And you should probably check that your arg to the create function is in bounds. Sorry to dissapoint you all with my lack of a witty or poignant signature.

                      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