Skip to main content
gamedev.net
🔒 Locked

class, pointer and multidimensionnal array

Started by zip7000 10 replies 350+ views
Original Post
zip7000
zip7000
hi, Here is my problem: I have two classes: Garden and Cell. the garden class has a set of 100 * 100 cells. How could I define that? I would like to use a pointer to a cell in the Garden class. So my class Garden Class Garden { Private: ... Cell* pcell; } //constructor Garden() { //what should I write here to create an array of 100 * 100 cells. } here my class cell Class Cell { Public: //constructor Cell() { val1=0; val2=0; } int GetVal1() { return val1; } } thank you for any help you can give zip7000
zip7000
zip7000
hi,

thank you for your answer. Unfortunatly it doesn''t work.

The file Garden.h


class Garden : public SpatialObject
{
public:
void RemovePheromoneOnCell(int x, int y);
void AddPheromoneOnCell(int x, int y);
void ReleaseItemOnCell(int x, int y);
void PutItemOnCell(int x, int y);
int GetWorldIndexY();
int GetWorldIndexX();
Garden();
~Garden();

private:
Cell* pcell;
int WorldIndexY;
int WorldIndexX;
};


the constructor in the file Garden.cpp

Garden::Garden() : SpatialObject(0,0,800,800)
{

WorldIndexY = 0;
WorldIndexX = 0;
pcell = new Cell[100][100];
}

here is the error message:
h:\antsproject\garden.cpp(17) : error C2440: ''='' : cannot convert from ''class Cell (*)[100]'' to ''class Cell *''

It works if I write pcell = new Cell[10000];
but I would like to use a multidimensional array to avoid to calculate the index.

zip7000
jwalker
jwalker
Allocate a block of memory to hold the
whole array first. Create an array of pointers to point
to each row. Thus even though the array of pointers is being
used, the actual array in memory is contiguous. The code looks
like this:


    
int main(void)
{
int **rptr;
int *aptr;
int *testptr;
int k;
int nrows = 5; /* Both nrows and ncols could be evaluated */
int ncols = 10; /* or read in at run time */
int row, col;
/* we now allocate the memory for the array */
aptr = malloc(nrows * ncols * sizeof(int *));
if(aptr == NULL)
{
puts("\nFailure to allocate room for the array");
exit(0);
}
/* next we allocate room for the pointers to the rows */
rptr = malloc(nrows * sizeof(int *));
if(rptr == NULL)
{
puts("\nFailure to allocate room for pointers");
exit(0);
}
/* and now we 'point' the pointers */
clrscr();
for(k = 0; k < nrows; k++)
{
rptr[k] = aptr + (k * ncols);
}
printf("\n\n\nIndex Pointer(hex) Pointer(dec) Diff.(dec)");

for(row = 0; row < nrows; row++)
{
printf("\n%d %p %d", row, rptr[row], rptr[row]);
if(row > 0)
printf(" %d",(int)(rptr[row] - rptr[row-1]));
}
for(row = 0; row < nrows; row++)
{
for(col = 0; col < ncols; col++)
{
rptr[row][col] = row + col;
printf("%d ", rptr[row][col]);
}
putchar('\n');
}
puts("\n\n\n");

/* and here we illustrate that we are, in fact, dealing with
a 2 dimensional array in a _contiguous_ block of memory. */


testptr = aptr;
for(row = 0; row < nrows; row++)
{
for(col = 0; col < ncols; col++)
{
printf("%d ", *(testptr++));
}
putchar('\n');
}
return 0;
}


i got this code many years ago, so its kinda bad in structure, but take your time reading it, its quite useful for making multidimensional arrays which have the performance of a single dimensional array.
Its my duty, to please that booty ! - John Shaft

[edited by - jwalker on August 5, 2002 9:40:29 PM]
silvermace
silvermace
Use std::vector
look it up, i think multi-dimension is

      
std::vector<std::vector<Garden> > // THE SPACE IS VERY IMPORTANT



[edit: use source tags with template code, and I think he'd want a vector<vector<Cell> >]

[edited by - Magmai Kai Holmlor on August 6, 2002 1:44:19 AM]
zip7000
zip7000
hello,

I tried to use the STL. I would like to use the vector class. But I am a beginner in this domain. Here that I did:

#include "Item.h"
#include <vector>
class Ant; <--- Must I do that?

using namespace std;

class Nest : public Item
{
public:
Nest(int x, int y, int width, int height);
virtual ~Nest();

private:
std::vector ants;
int IdNum;
};


my goal is to create a vector of ants.

here the error message that I get:

c:\program files\microsoft visual studio\vc98\include\vector(59) : error C2036: ''class Ant *'' : unknown size
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function ''__thiscall std::vector >::~std::vector >(v
oid)''

any suggestion?
ffiorino
ffiorino
You need to define the type of elements you Ants vector is holding, for example std::vector m_Ants
TangentZ
TangentZ
class "Ant" must be defined before you can write


  
class Ant { /* blah */ };
std::vector<Ant> ants;


You may get around this by using


  
class Ant;
std::vector<Ant *> ants;
class Ant { /* blah */ };


The usual caveats when dealing with pointers apply.



Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.