Array 2d in header but in object errors :(

Hey guys im just having a bit of trouble with my game project,
Im trying to make a 2d array for a grid, this grid will be used to move the character around and take the location of the hero and it wil check if there is a wall blocking the characters movement or etc etc,

Now i want the 2d array to be global as methods will be accessing it,
here is my dungeon.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class firstDungeon {

  private:
	  //variables
	  int x;
	  int y;
	  int location;
	  string option;
	  int dungeonWalls[20][20];
	  int * walls; //pointer
	  int finish;//if dungeon is done
	  //map Cords

	  
  
  public:
	  //methods
	  int move(string, hero*);
	  void setLocation(int);
	  void setDungeon();
	  void control(string, hero*);
	  void updateLocation();
	  int returnFinish();
	  int getY();
	  int getX();
	  int checkWalls(int);
	  void potionOption();
	  int returnLocation();
	  void mapDisplay();
	  void createDungeon(int);

};

now here is the method that will give information to the 2d array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void firstDungeon::setLocation(int dungeon){
	if (dungeon == 1){
	finish = 0;
	x = 1;
	y = 1;
	dungeonWalls[20][20] = {

	   //0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9
		{1,1,1,0,1,1,1,1,1,9},//0floor, 1wall, 2well					   
		{1,0,1,0,1,0,0,3,1,9},//3stairs, 4message,5
		{1,0,1,1,1,0,1,1,1,9},//6,7,8stop drawing map
		{1,0,0,0,0,0,0,1,0,9},//9end line in console.
		{1,1,1,0,1,1,0,1,0,9},
		{1,0,0,0,1,1,0,1,0,9},
		{1,2,1,0,0,0,0,1,0,9},
		{1,1,1,1,1,1,1,1,9,8},
	};
	}
	//if (dungeon == 2){
	//finish = 0;
	//x = 1;
	//y = 1;
	//}
}

now on the line
 
dungeonWalls[20][20] = {

the { displays this
error expected an expression
what am i doing wrong?

Thank you

Doom666
Last edited on
You can't assign arrays, maybe an array of strings would be more appropriate for you.

Please use [code][/code] tags
Last edited on
I want it to be ints so in my method checker it will checker what is in section x and y and see what it is, so then if its a 0 then the hero can move south, how else can i assign this array then to be a global?

thanks for the reply
You can check individual elements of a string as well
If you want to keep an int array you can initialize a temporary array and initialize with the { } syntax, then with a nested loop, modify all the elements in dungeonWalls to match those of the temporary array.
for example?
Like this:
1
2
3
4
5
6
7
8
int temp = { 
    {1,1,1,0 // ...
    // ...
};

for ( int i = 0; i < 20; i++ )
    for ( int j = 0; j < 20; j++ )
        dungeonWalls[i][j] = temp[i][j];

Where temp is initialized as you were trying to assign to dungeonWalls, then the two loops access every element of the arrays and copy temp into dungeonWalls.

The alternative would be to assign each element of dungeonWalls
1
2
3
dungeonWalls[0][0] = 1;
dungeonWalls[0][1] = 1;
//... 
but that would be very tedious and unreadable
I think the error is within your design, rather than within your arrays (yeah, of course you can't do the assignment like that, but that is the least of your problems here).
nice i will give it try once i get back, ive got a lecture to go to, cheers for the help :p
this is the code i have at the mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int temp [][] = {
		{1,1,1,0,1,1,1,1,1,9,0,0,0,0,0,0,0,0,0,0},
		{1,0,1,0,1,0,0,3,1,9,0,0,0,0,0,0,0,0,0,0},
		{1,0,1,1,1,0,1,1,1,9,0,0,0,0,0,0,0,0,0,0},
		{1,0,0,0,0,0,0,1,0,9,0,0,0,0,0,0,0,0,0,0},
		{1,1,1,0,1,1,0,1,0,9,0,0,0,0,0,0,0,0,0,0},
		{1,0,0,0,1,1,0,1,0,9,0,0,0,0,0,0,0,0,0,0},
		{1,2,1,0,0,0,0,1,0,9,0,0,0,0,0,0,0,0,0,0},
		{1,1,1,1,1,1,1,1,1,9,8,0,0,0,0,0,0,0,0,0},
	};
	for ( int i = 0; i < 20; i++ ){
    for ( int j = 0; j < 20; j++ ){
        dungeonWalls[i][j] = temp[i][j];
	}
	}


but it doesnt like the second [] at the temp[][]
any reason?

its ok i just put in 20,20 and also closed a bracket off in my program, it seems to work fine, thanks for the help Bazzy
Last edited on
In case you care: When declaring a n dimensional array, n-1 dimensions must be constant because they can't be deducted on runtime.
Topic archived. No new replies allowed.