Accesing a Mutli-Dimensional Array from another Class file

Hey guys, about a week ago I started coding a Roguelike.
I've been having structure problems for a while and have sort of been skirting around them.
A feature very central to RLs is the Map drawing, and I have chosen to implement this using Mulid-Dimensional arrays. I also like to keep my Classes in separate files, so most of the Classes that are interaction based or Map based are requiring this array. Basically, I have been swarmed with "'array' was not declared in this scope" errors.

Here's the code:

Main.cpp
1
2
3
4
5
6
7
8
9
#include "Map.h"

int main ()
{
   Map printMapOb;

   printMapOb.generateMap();
   printMapOb.drawMap(&GenMap, &M_ROW, &M_COL);
}


Map.h
1
2
3
4
5
6
7
class Map
{
    public:
        int generateMap();
            int *GenMap[][0];
        void drawMap(int mapArray[][0], int M_R, int M_C);
}


Map.cpp
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
#include "Map.h"
#define TILE_FLOOR      0

int Map::generateMap(){

    int *M_COL, *M_ROW;

    int row, col;

    srand(time(0));
    *M_ROW = (rand()%10) + 20;
    *M_COL = (rand()%10) + 20;

    int *GenMap[*M_ROW][*M_COL];

    for (row = 0; row < *M_ROW; row++)
    {
        GenMap[row][col] = TILE_FLOOR;

        for (col = 0; col < *M_COL; col++)
        {
            GenMap[row][col] = TILE_FLOOR;
        }
    }
}


So basically we've got generateMap() generating a Map, go figure. drawMap() prints the map to the screen, and requires that an array and two ints be passed to it. My problem here is I dont understand how other RLs can have a large amount of functions and classes interacting with the map, when all of those functions and classes are split up between multiple files, and are having to share this one Map.

Current Error:
H:\Code\Roguelike Dev\Ismuth\main.cpp|63|error: 'GenMap' was not declared in this scope|
H:\Code\Roguelike Dev\Ismuth\main.cpp|63|error: 'M_ROW' was not declared in this scope|
H:\Code\Roguelike Dev\Ismuth\main.cpp|63|error: 'M_COL' was not declared in this scope|

Any help much appreciated!
EDIT: I omitted a lot of the code for the sake of simplicity.
Last edited on
Just wondering about these:

int *GenMap[][0];

int mapArray[][0]

Why the zero dimension? It is effectively a 1 dimension array.

Edit:

The errors are because you have no variables GenMap, M_ROW, or M_COL in main().

M_ROW, or M_COL are defined in Map.cpp, they should be defined in Map.h

GenMap needs an object qualifier. You can't refer to member variables directly. Also an array name is a pointer so maybe GenMap doen't need the & operator when it is an argument in a function call.
Last edited on
As far as I know, the arrays don't require the definitions of their dimensions because they're prototypes. And if I were to define the values for them, I would have to declare the variables that I normally place in there within the header file.
Last edited on
Again as far as I know, only members of a Class need to be declared in the Header file. Variables are not valid members. Same thing with the object qualifier, object qualifiers are only valid on members of the stated class.
As far as I know, the arrays don't require the definitions of their dimensions because they're prototypes. And if I were to define the values for them, I would have to declare the variables that I normally place in there within the header file.


OK, I am saying you can do this:

int mapArray[][]

but this doesn't make sense:

int mapArray[][0]

I am fairly sure that a zero subscript is an error. If not an error, then it is pointless.

I don't think you are getting what I am saying with regard to M_ROW, or M_COL.

They are local variables to the function generateMap(), so you can't refer to them in main()

If you want to use them in main() then declare them in main.

Same thing with the object qualifier, object qualifiers are only valid on members of the stated class.


GenMap is member variable of the class Map, so you need to access it through an object.

The only other way to use a member variable, is by reference to it from within a class function.

You cannot refer to a member variable the way you have done it.

HTH
Right-o, thanks for the help.
Topic archived. No new replies allowed.