【问题标题】:Having Trouble with 2-Dimensional Array of Structs C++遇到二维结构数组 C++ 的问题
【发布时间】:2014-01-25 15:55:51
【问题描述】:

我已经搜索了这个网站和谷歌,并没有真正找到任何可以解决我问题的东西。我现在正在尝试编写游戏,该游戏包含玩家可以移动的地形图块地图。我想将图块存储在 10x10 数组中,但在初始化数组时遇到了问题。

我可以初始化数组的第一个维度,但错误在于在第一个 for 循环中初始化第二个维度。

我的代码如下所示:

//tile on the "map"
struct tile
{
    char type;
    bool isWall;
};

void initializeMap(tile * map)
{
  int index1, index2;

  for(index1 = 0; index1 < 10; index1++)
  {
    map[index1] = new tile[10];

    for(index2 = 0; index2 < 10; index2++)
    {

    }
  }
}

int main()
{
    tile * tileMap = new tile[10];
    initializeMap(tileMap);

    return 0;
}

我收到此错误:

C:\Users\----\Desktop\TextGame.cpp||In function 'void initializeMap(tile*)':|
C:\Users\----\Desktop\TextGame.cpp|39|error: no match for 'operator=' in '*(map + ((unsigned int)(((unsigned int)index1) * 2u))) = (tile*)operator new [](20u)'|
C:\Users\----\Desktop\TextGame.cpp|9|note: candidates are: tile& tile::operator=(const tile&)|
||=== Build finished: 1 errors, 0 warnings ===|

【问题讨论】:

  • 不需要tile**,因为您要将tile* 打包到地图中吗?
  • @zero298 很确定这就是问题所在,清理一下并将其作为答案发布
  • 我建议更改变量名,因为std::map 已经存在并且map 的变量名可能会导致冲突。

标签: c++ arrays multidimensional-array struct


【解决方案1】:

我建议使用std::vector 共享指向磁贴的指针。这将允许您拥有不同的图块并将它们存储到向量中:

struct Tile; // As you declared it.

struct Water_Tile : public Tile;
struct Mountain_Tile : public Tile;
struct Desert_Tile : public Tile;

// ...
typedef std::vector< boost::shared_ptr<Tile> > Tile_Container;

//...
Tile_Container    my_world(10 * 10);

// ...
my_world[20] = new Water_Tile;

共享指针的一个很好的优点是它可以为您处理内存管理(删除)。

【讨论】:

  • 这很有趣。语法看起来有点吓人,因为我还没有真正费心学习 std::vector 。它们可以即时调整大小,对吗?
  • std::vector的大小可以在创建时设置,也可以在使用push_back方法时根据需要扩展。
【解决方案2】:

注意:从技术上讲,这不是二维数组,而是结构数组的数组。

也就是说,那个注释反映了你的代码的问题:你有一个数组数组,所以第一次分配应该分配一个数组,其内容是指向其他数组的指针

tile** tilemap = new tile*[10];

for( std::size_t i = 0 ; i < 10 ; ++i )
    tilemap[i] = new tile[10];

....

//Dealocation at program finish:

for( std::size_t i = 0 ; i < 10 ; ++i )
    delete tilemap[i];

delete tilemap;

但是,如果您使用标准容器(例如 std::vector)而不是容易出错的手动内存管理,您的代码可能会得到改进:

std::vector<std::vector<tile>> tilemap( 10 );

for( std::size_t i = 0 ; i < 10 ; ++i )
    tilemap.emplace_back( 10 );

【讨论】:

  • +1 建议不要使用内存分配。
【解决方案3】:

您正在尝试使用以下命令将实际对象设置为指针:

map[index1] = new tile[10];

maptile*。然而,map[index1] 是一个被尊重的tile*,使其实际上是一个tile,它不能等于new tile[10] 给你的tile*

因此,您的代码将更好地工作为:

struct tile {
   char type;
   bool isWall;
};

/**
 * Initialize the map
 * @param map The array of tile pointers
 */
void initializeMap(tile** map) {
   int index1, index2;
   for (index1 = 0; index1 < 10; index1++) {

      // Set each element of the tile* array 
      // to another array of tile pointers
      map[index1] = new tile[10];

      for (index2 = 0; index2 < 10; index2++) {
         // Do Something
      }
   }
}

int main() {
   // Create a pointer to a set of tile pointers
   tile** tileMap = new tile*[10];
   // Pass it to the initializer
   initializeMap(tileMap);
   return 0;
}

【讨论】:

  • 非常感谢。这更有意义。当我第一次尝试时,我试图将 tileMap 设为双指针,但语法不正确。
猜你喜欢
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2013-10-26
  • 1970-01-01
  • 2012-02-28
  • 2021-09-22
  • 1970-01-01
相关资源
最近更新 更多