CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Need help with 2d array o.O

    hey folks, i need some help with a 2d array that i am using ->

    i have a class with the member variables

    protected:
    //member variables
    int m_iScreenWidth, m_iScreenHeight;
    int m_iTilesAcross, m_iTilesDown;
    int m_iTileWidth;

    GameEngine* m_pGame;
    Bitmap** m_pBitmapGrid;

    then i have in the constructor for the class i have:

    Grid::Grid(int iTileWidth, GameEngine*_pGame)
    {
    m_pGame = _pGame;
    m_iTileWidth = iTileWidth;
    m_iScreenWidth = m_pGame->GetWidth();
    m_iScreenHeight = m_pGame->GetHeight();
    m_iTilesAcross = m_iScreenWidth / m_iTileWidth;
    m_iTilesDown = m_iScreenHeight / m_iTileWidth;
    m_pBitmapGrid = new Bitmap*[m_iTilesAcross];
    }

    Its a very simple class atm, and i plan to expand on it later, but currently i know that the get height and getWidth functions being called work. And all the code above works properly, however the fault comes when i try to make the array into a 2D array:

    m_pBitmapGrid = new Bitmap*[m_iTilesAcross][m_iTilesDown];

    i get the following compile errors related to the line above:

    error C2540: non-constant expression as array bound

    error C2440: '=' : cannot convert from 'Bitmap *(*)[1]' to 'Bitmap **'

    the two variables that define the size of the array are of the same type and are set to a specific value in the constructor. Even when i made the 2nd dimention of the array [m_iTilesDown] it still didnt compile and gave me the same errors.

    But i know that m_iTilesAcross should count as a constant integer becuase it works fine when initializeing just a 1 dimensional array. So why is it that a single dimention array works and a 2d array dosnt. And is there any way around this?

    Any help is much appriciated, thank you.

  2. #2
    Join Date
    Oct 2008
    Posts
    116

    Re: Need help with 2d array o.O

    Code:
    m_pBitmapGrid = new Bitmap*[m_iTilesAcross];
    for(int i = 0; i < m_iTilesAcross; ++i)
      m_pBitmapGrid[i] = new Bitmap[m_iTilesAcross];
    Try to use it.
    My English is very bad. So tell me if Something I talked make u confuse.
    My Ebook Store: www.coding.vn/book.php

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured