Idea is that MapGrid creates ands holds 2d array with map elements and I want to give that array to another class MapGenerator so it generates a real map by editing square elements in it.
class MapGenerator {
private:
int MAP_HEIGHT;
int MAP_WIDTH;
Square * worldMap;
void cmdGenerateHeightMap() {
worldMap[i][j]->decraseHeight();
......
}
public:
MapGenerator(Square &myMap, int maxHeight, maxWidth) {
MA开发者_运维百科P_WIDTH = maxWidth
MAP_HEIGHT = maxHeight;
worldMap = &myMap;
cmdGenerateHeightMap();
}
}
class MapGrid {
private:
static const int MAP_HEIGHT = 100;
static const int MAP_WIDTH = 100;
Square worldMap[MAP_HEIGHT][MAP_WIDTH];
public:
MapGrid() {
for (int i=0; i<MAP_HEIGHT;i++) {
for (int j=0;j<MAP_WIDTH; j++) {
worldMap[i][j] = Square();
}
}
MapGenerator myMap(worldMap);
}
void PrintMyMap() {
//print what the map MapGenerator made
}
}
I get this error: variable ‘MapGenerator myMap’ has initializer but incomplete type map.h Could I get some more human sounding hints what is wrong
Swap the order in which the MapGenerator
and MapGrid
classes are defined. You're trying to use the MapGenerator
class inside the MapGrid
constructor, but that class is only defined further below.
UPDATE: Okay, now that you rewrote your question, here's another guess (your code doesn't actually compile, so I can only guess): is it possible that MapGenerator
and MapGrid
are in different header files and in the header file of MapGrid
you only have a forward declaration of MapGenerator
instead of the actual definition (which you would get by including MapGenerator
's header)?
I don't think that you want to be passing worldMap
in the MapGrid constructor method when it hasn't been initialized to anything yet.
In order to instantiate an object for a class, you need the full information of the class. But in the class MapGrid
, trying to instantiate an object for MapGenerator
is invalid in the current form of code snippet ordering. Instead, try this -
class MapGenerator
{
// .....
};
class MapGrid
{
// ....
};
Following code is similar to yours just after commenting the error prone line. Can you tell me what was its utility?.
class MapGenerator {
private:
Square * worldMap;
void cmdGenerateHeightMap() {
//do something to the orginal copy of worldMap
}
public:
MapGenerator(Square &myMap) {
worldMap = &myMap;
cmdGenerateHeightMap();
}
};
class MapGrid {
private:
static const int MAP_HEIGHT = 100;
static const int MAP_WIDTH = 100;
Square worldMap[MAP_HEIGHT][MAP_WIDTH];
public:
MapGrid() {
for (int i=0; i<MAP_HEIGHT;i++) {
for (int j=0;j<MAP_WIDTH; j++) {
worldMap[i][j] = Square();
}
}
//commented following line
//MapGenerator myMap(worldMap);
}
void PrintMyMap() {
//print what the map MapGenerator made
}
};
I think MapGenerator
doesn't need to be a class.
Probably cmdGenerateHeightMap
can be a free standing function, instead of
a member function.
Alternatively, in order to simplify the access to two dimensional array,
I'd suggest making a dedicated class, for example TwoDimArray
.
How about calling cmdGenerateHeightMap
from MapGrid
's constructor like the
following?:
struct TwoDimArray {
int MAP_WIDTH;
Square *worldMap;
TwoDimArray( Square* m, int w ) : worldMap( m ), MAP_WIDTH( w ) {}
Square* operator[]( int i ) const { return worldMap + MAP_WIDTH * i; }
};
void cmdGenerateHeightMap( Square *myMap, int maxHeight, int maxWidth )
{
TwoDimArray worldMap( myMap, maxWidth );
...
worldMap[i][j].decraseHeight();
...
}
class MapGrid {
...
MapGrid() {
...
cmdGenerateHeightMap( worldMap[0], MAP_HEIGHT, MAP_WIDTH );
}
};
Hope this helps
精彩评论