Thread: Dynamically SIzed Global Matrix problems

  1. #1
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109

    Dynamically SIzed Global Matrix problems

    I'm working on a game that uses a class which creates a matrix for a bunch of tiles. What i want to do, is to be able to pass to the class, when it is created, the xtiles, and ytiles, for example:

    int *matrix;

    void setup(int xtile, int ytile)
    {
    matrix=new int[ytile] [xtile];
    }

    void main()
    {
    setup(30,40);
    }

    and that would resize the array to be 30 across, and 40 down, but when i compile in Visual C++, it says something about how it can't convert from one int to another.......if you know how to fix this, I would be greatful for a reply, thank you!
    int a; don't make a program without it.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You can't directly dynamically allocate a multidimensional array.

    What you want to do is allocate an array of size numrows times numcols and access indices in it by RowToAcces * NumCols + ColToAccess.

    Or you can allocate an array of size numcols which would be pointers to the datatype you are making an array of and then dynamically allocate each column individually, but this method is not a good idea.

    Also, don't use void main use int main. kthxbai.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Code:
    int *matrix;
    int Width, Height;
    
    void setup (int xtile, int ytile)
    {
          matrix = new int[ytile*xtile];
          Width = xtile;
          Height = ytile;
    }
    
    __inline int GetTile (int x, int y) 
    {
        return matrix[(y * Width) + x];
    }
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  2. Problems Accessing Matrix
    By Maragato in forum C Programming
    Replies: 6
    Last Post: 05-09-2005, 03:11 PM
  3. Problems putting a text file into a matrix.
    By warny_maelstrom in forum C Programming
    Replies: 15
    Last Post: 01-22-2004, 06:33 PM
  4. problems with matrix mult. method
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2003, 09:05 AM
  5. Problems with Global Structure
    By bijju_savan in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 03:37 PM