Thread: Creating object arrays for VC++

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    18

    Creating object arrays for VC++

    I need to create an array of objects when I click a button in Visual C++. I am having some difficulty doing so. Any help would be appreciated.

    ---Code within button click---
    Code:
    PictureBox* picBlocks[];
    
    for(int i = 0; i < 5; i++)
    {
    
    picBlocks[i] = new PictureBox();
    
    pnlGameField->Controls->Add(picBlocks[i]);
    
    }
    When I run it, it gives me an error about an unhandled exception.

    Debug stops at the last line of code. I'm still quite a beginner, so I'm not exactly sure what to look for.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You don't allocate any space for the pointer array.
    Wouln't that be enough ?
    Code:
    for(int i = 0; i < 5; i++) 
         pnlGameField->Controls->Add(new PictureBox() );
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Are you saying that this should be my only line of code:

    Code:
    for(int i = 0; i < 5; i++) 
         pnlGameField->Controls->Add(new PictureBox() );
    I need each new PictureBox to be named picBlocks[i].

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    PictureBox* picBlocks[];
    should be
    PictureBox* picBlocks[5];
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    When I add [5] instead of [], I get the error at this line:
    Code:
    PictureBox* picBlocks[];
    error C3616: '5': a size cannot be specified in a __gc array declaration

    and

    at this line:
    Code:
    picBlocks[i] = new PictureBox();
    error C3262: invalid array indexing: 1 dimension(s) specified for 5-dimensional 'System::Windows::Forms::PictureBox __gc * __gc[,,,,]'
    Last edited by tineras; 05-06-2006 at 09:06 AM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by tineras
    Are you saying that this should be my only line of code:

    Code:
    for(int i = 0; i < 5; i++) 
         pnlGameField->Controls->Add(new PictureBox() );
    Yes that is what I mean
    Quote Originally Posted by tineras
    I need each new PictureBox to be named picBlocks[i].
    I think if you still have to access the the PictureBoxes in the same function then there should be a way to access them via pnlGameField->Controls
    Even if you do as Shakti says the pointer- array would go out of scope after return from that function.
    Kurt

    EDIT: Just saw that this is not C++ ( seems to be some garbage collector involved ) so I actually don't know anything about scopes in your system.
    Last edited by ZuK; 05-06-2006 at 09:10 AM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Code:
    for(int i = 0; i < 5; i++) 
    pnlGameField->Controls->Add(new PictureBox() );
    This seems to work without error, but I just don't know how to access the new PictureBoxes that are created.

    Like PictureBox[0] or PictureBox[1] or whatever they would be called so that I can use them how I wish.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    When I add [5] instead of [], I get the error at this line:
    Code:

    PictureBox* picBlocks[];


    error C3616: '5': a size cannot be specified in a __gc array declaration
    This is funny. But as I said don't know anything about this "__gc" thingy.
    But I guess that should work
    Code:
    for(int i = 0; i < 5; i++)  {
        PictureBox* picBlock = new PictureBox();
        pnlGameField->Controls->Add( picBlock );
       // here you can work with the newly created block
    }
    Kurt

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by ZuK
    This is funny. But as I said don't know anything about this "__gc" thingy.
    But I guess that should work
    Code:
    for(int i = 0; i < 5; i++)  {
        PictureBox* picBlock = new PictureBox();
        pnlGameField->Controls->Add( picBlock );
       // here you can work with the newly created block
    }
    Kurt
    That seems to be running without any problems also, but I'm not sure what each of these new PictureBoxes are being called. I don't know how reference them in my code...

    like:

    Code:
    picBlocks1->Top = 100;
    
    or
    
    PictureBox[1]->Left = 50;
    I don't know how to use it once it's created.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In my previous example you would have to do something like this.
    Code:
    for(int i = 0; i < 5; i++)  {
        PictureBox* picBlock = new PictureBox();
        pnlGameField->Controls->Add( picBlock );
        switch ( i ) {
           case 0: 
                picBlock->Top = 100;
                picBlock->Left = 50;
                break;
           case 1: 
                picBlock->Top = 150;
                picBlock->Left = 50;
                break;
      //       ......
         }
    }
    I am aware that this is just a workaround. Even in a garbage collected system ther must be a way to create an array of pointers.
    Kurt

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by ZuK
    In my previous example you would have to do something like this.
    Code:
    for(int i = 0; i < 5; i++)  {
        PictureBox* picBlock = new PictureBox();
        pnlGameField->Controls->Add( picBlock );
        switch ( i ) {
           case 0: 
                picBlock->Top = 100;
                picBlock->Left = 50;
                break;
           case 1: 
                picBlock->Top = 150;
                picBlock->Left = 50;
                break;
      //       ......
         }
    }
    I am aware that this is just a workaround. Even in a garbage collected system ther must be a way to create an array of pointers.
    Kurt
    I tried this and it does work. However, I am still unable to use these items in other functions in my code. I really need to be able to reference them individually without having to jump through a lot of hoops to make it work.

    Aaron

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You aren't passing anything to the constructors, so what's wrong with:
    Code:
    PictureBox* picBlocks = new PictureBox[5];
    
    for(int i = 0; i < 5; i++)
       pnlGameField->Controls->Add(picBlocks[i]);
    EDIT: Oh, wait. Do you want an array of pointers?

    Maybe change your "PictureBox* picBlocks[]" to "PictureBox** picBlocks" and allocate the same way. Like this:
    Code:
    PictureBox **picBlocks = new PictureBox*[5]; // Pointer to array of 5 pointers
    
    for(int i = 0; i < 5; i++)
       pnlGameField->Controls->Add(picBlocks[i]);
    EDIT 2: Sorry for all the edits, hope I'm not screwing with your head if you're looking at it right now.
    Last edited by SlyMaelstrom; 05-06-2006 at 02:20 PM.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by SlyMaelstrom
    You aren't passing anything to the constructors, so what's wrong with:
    Code:
    PictureBox* picBlocks = new PictureBox[5];
    
    for(int i = 0; i < 5; i++)
       pnlGameField->Controls->Add(picBlocks[i]);
    EDIT: Oh, wait. Do you want an array of pointers?

    Maybe change your "PictureBox* picBlocks[]" to "PictureBox** picBlocks" and allocate the same way. Like this:
    Code:
    PictureBox **picBlocks = new PictureBox*[5]; // Pointer to array of 5 pointers
    
    for(int i = 0; i < 5; i++)
       pnlGameField->Controls->Add(picBlocks[i]);
    EDIT 2: Sorry for all the edits, hope I'm not screwing with your head if you're looking at it right now.

    When I put this code in:
    Code:
    PictureBox* picBlocks = new PictureBox[5];
    
    for(int i = 0; i < 5; i++)
       pnlGameField->Controls->Add(picBlocks[i]);
    I get a bunch of errors:
    ---------------------------
    error C2691: 'System::Windows::Forms::PictureBox' : invalid type for __gc array element

    error C2691: 'System::Windows::Forms::PictureBox __gc *' : invalid type for __gc array element

    error C3149: 'System::Windows::Forms::PictureBox' : illegal use of managed type 'System::Windows::Forms::PictureBox'; did you forget a '*'?

    error C3262: invalid array indexing: 1 dimension(s) specified for 5-dimensional 'System::Windows::Forms::PictureBox __gc * __gc[,,,,]'

    error C3616: '5': a size cannot be specified in a __gc array declaration
    --------------------------

    Is there something missing at the top of my code that I might need for this to work.

    I have:
    Code:
    #pragma once
    #include <iostream>
    #include <cmath>
    #include <math.h>
    using namespace std;

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why are you including both cmath and math.h? I'd imagine if you opened math.h, you would see a small library that includes cmath and directs the use of all of it's functions under the std namespace.

    Anyway, I don't know the API you are using, so I'm not familiar with the add() method you have there. Does it take a PictureBox or a PictureBox*? If it's the latter, then use the second code I gave you.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There seem to be very different rules for pointer arrays in "managed C++" code. Guess you should look for a forum that specifically deals with this kind of "C++ extension"
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating A Thread W/ PThreads within an Object
    By HalNineThousand in forum Linux Programming
    Replies: 12
    Last Post: 03-28-2008, 02:57 PM
  2. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  3. Deleting Dynamic array's of an object
    By j0hnb in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 02:25 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM