C ++通过引用传递2d数组到其他类

时间:2011-03-09 11:25:28

标签: c++ arrays class reference

想法是MapGrid创建和使用地图元素保存2d数组,我想将该数组提供给另一个类MapGenerator,以便通过编辑其中的方形元素来生成实际地图。

class MapGenerator {
        private:
            int MAP_HEIGHT;
            int MAP_WIDTH;
            Square * worldMap;
            void cmdGenerateHeightMap() {
             worldMap[i][j]->decraseHeight();
              ......
            }
        public:
        MapGenerator(Square &myMap, int maxHeight, maxWidth) {
                MAP_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
            }
    }

我收到此错误:变量'MapGenerator myMap'具有初始化程序但不完整的类型map.h 我可以得到一些更人性化的提示吗?

5 个答案:

答案 0 :(得分:1)

交换定义MapGeneratorMapGrid类的顺序。您正尝试在MapGenerator构造函数中使用MapGrid类,但该类仅在下面进一步定义。

更新:好的,既然你重写了你的问题,这是另一个猜测(你的代码实际上没有编译,所以我只能猜测):是否有可能MapGeneratorMapGrid位于不同的头文件中,MapGrid的头文件中只有MapGenerator的前向声明而不是实际定义(您可以通过包含MapGenerator来获得的标题)?

答案 1 :(得分:0)

我认为你还没想在MapGrid构造函数方法中传递worldMap时还没有初始化它。

答案 2 :(得分:0)

为了实例化一个类的对象,您需要该类的完整信息。但是在类MapGrid中,尝试为MapGenerator实例化对象在当前的代码段排序形式中是无效的。相反,试试这个 -

class MapGenerator
{
    // .....
};

class MapGrid
{
    // ....
};

答案 3 :(得分:0)

在评论容易出错的行之后,以下代码与您的代码类似。你能告诉我它的用途是什么吗?。

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
        }


};

答案 4 :(得分:0)

我认为MapGenerator不需要成为一个班级 可能cmdGenerateHeightMap可以是一个独立的功能,而不是 会员功能。
或者,为了简化对二维阵列的访问, 我建议你做一个专门的课程,例如TwoDimArray 如何从cmdGenerateHeightMap的构造函数中调用MapGrid 以下:

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 );
  }
};

希望这有帮助