Creating "map", problem with array in a class?

Okay, so I'm working on a school project. It's a text-based RPG, and I want to have a "map" that the player can traverse through (not a physical map that shows up on the screen, mind you. Something that tracks the player. Like:

"You are at spot [1,0]. Which way you like to go?

User inputs E (for East)

"You are at spot [2,0]."

That.)

It's supposed to be designed like a chessboard (aka 8 x 8). To this end, I'm trying to use an array so I can track the player's position and any enemy/item's position using "XY" co-ordinates. However, the array just won't work, and I've been working on this for at least a day now. Below is what I currently have (thanks to classmates):

Map1.h

#ifndef MAP1_H
#define MAP1_H

using namespace std;

class Map1
{
public:
Map1();
char gMap01() const;
void sMap01 (char);

private:
int X = 8;
int Y = 8;

char map01[X][Y] = {{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'}};
};

#endif


Map1.cpp

#include "Map1.h"

Map1::Map1 () {}

char Map1::gMap01() const
{
return this->map01[X][Y];
}

void Map1::sMap01 (char newValue)
{
this->map01[X][Y] = newValue;
}


I get errors:

Error 1 error C2864: 'Map1::X' : only static const integral data members can be initialized within a class e:\c++\projects\myRpg\myRpg\map1.h 15
Error 3 error C2327: 'Map1::X' : is not a type name, static, or enumerator e:\c++\projects\myRpg\myRpg\map1.h 18
Error 4 error C2065: 'X' : undeclared identifier e:\c++\projects\myRpg\myRpg\map1.h 18
Error 9 error C2039: 'map01' : is not a member of 'Map1' e:\c++\projects\myRpg\myRpg\map1.cpp 7


Not quite sure what to do from here on in, and I'm feeling particularly frustrated. I'm feeling the answer is clearly obvious, but just not to me and that I'm looking in the wrong place(s). Can anybody help?
closed account (o3hC5Di1)
Hi there,

Try doing the following:

Map1.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MAP1_H
#define MAP1_H

using namespace std;

class Map1
{
public:
Map1();
char gMap01() const;
void sMap01 (char);
char map01[8][8]; //added declaration of map array here

private:
int X;
int Y;
};

#endif 


Map1.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Map1.h"

//Initiialise the map in the constructor
Map1::Map1 () : X(8), Y(8)
{
    for (int i=0; i<8; i++)
    {
        for (int ii=0; ii<8; ii++)
        {
            map01[i][ii] = 'O';
        }
    }
}

char Map1::gMap01() const
{
return this->map01[X][Y];
}

void Map1::sMap01 (char newValue)
{
this->map01[X][Y] = newValue;
}


Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.