【问题标题】:C++ passing 2d array by reference to other classC ++通过引用其他类传递二维数组
【发布时间】:2011-03-09 11:25:28
【问题描述】:

想法是 MapGrid 创建并保存带有地图元素的二维数组,我想将该数组提供给另一个类 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 我可以得到一些更人性化的提示什么是错的

【问题讨论】:

  • “MapGenerator myMap(worldMap);”在 MapGrid 方法中的作用是什么?

标签: c++ arrays class reference


【解决方案1】:

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

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

【讨论】:

  • 做到了,真正的问题是我认为我错误地传递了数组
  • 是的,两个类都在不同的文件中。 main.cpp 包含 header.h 并且在 header.h 中有两个类的前向声明和每个类头文件的#include。 MapGeneration 前向声明和#include 出现在 MapGrid 之前
【解决方案2】:

我认为您不想在 MapGrid 构造方法中传递 worldMap,因为它还没有被初始化为任何东西。

【讨论】:

    【解决方案3】:

    为了实例化一个类的对象,你需要类的全部信息。但是在类MapGrid 中,尝试为MapGenerator 实例化一个对象在当前的代码sn-p 排序形式中是无效的。相反,试试这个 -

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 好主意是我 MapGid 制作默认地图。平坦,没有河流(Square 拥有该信息)。 MapGenerator 采用默认地图并添加山脉河流和其他一些东西。然后它将地图返回给 MapGrid 以在游戏中使用。
      【解决方案5】:

      我认为MapGenerator 不需要是一个类。
      可能cmdGenerateHeightMap 可以是一个独立的函数,而不是 成员函数。
      或者,为了简化对二维数组的访问, 我建议开设一个专门的课程,例如TwoDimArray
      MapGrid 的构造函数调用cmdGenerateHeightMap 怎么样 关注?:

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

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-24
        • 1970-01-01
        • 2010-10-06
        • 2012-08-05
        • 2020-12-16
        相关资源
        最近更新 更多