Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,912 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,558 people online right now. Registration is fast and FREE... Join Now!




dynamic allocation of 2d array of pointers to a user defined type

 
Reply to this topicStart new topic

dynamic allocation of 2d array of pointers to a user defined type

Elkhirsawy
9 May, 2008 - 10:13 PM
Post #1

New D.I.C Head
*

Joined: 9 May, 2008
Posts: 1

i am making a game and i have a general/base class called GameObject

i need to make dynamicly a 2-d array of pointers to GameObject and to intiallize it to null and i have done the following:

//in headerfile

GameObject ***h;

//in source file
//deaflt constructor defining unused values for x and y dimensions
MapMaker::MapMaker()
{
int x=5,y=5;
setfileName("Beginner.txt");

}

// function tht create the 2d array and intiallize it to null
void MapMaker::createMap(int xdimension, int ydimension)
{
h = new GameObject**[xdimension];
for (int i = 0; i<xdimension; i++)
h[i] = new GameObject*[ydimension];

for(int j=0;j<xdimension;j++)
{
for(int l=0;l<ydimension;l++)
{
h[j][l]=NULL;


}
}
cout<<"Map Created"<<endl;
}

it does not give a syntax error bt after the game was made objects move correctly on x dimension only.

User is offlineProfile CardPM
+Quote Post

skaoth
RE: Dynamic Allocation Of 2d Array Of Pointers To A User Defined Type
10 May, 2008 - 01:40 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
You need to modify your code to do this instead

CODE

GameObject **h = NULL;
....

h = new GameObject*[xdimension];
for (int i = 0; i<xdimension; i++)
{
    h[i] = new GameObject[ydimension];
}


Somehow you have an extra '*' in your deceleration. The for loop above
will create the objects not pointers. If you want pointers instead then simply change it. But remember if you want an array of pointers for the y dimension that you will have to new those objects as as well
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month