简体   繁体   English

C维二维数组存在问题

[英]Having Trouble with 2-Dimensional Array of Structs C++

I have searched this website and Google, and haven't really found anything that solves my problem. 我已经搜索过该网站和Google,但还没有找到能解决我问题的任何东西。 I am trying to write a game right now, and this game contains a map of terrain tiles that the player can move across. 我正在尝试编写一个游戏,该游戏包含玩家可以穿越的地形图。 I want to store the tiles in a 10x10 array, but I am having trouble initializing the array. 我想将图块存储在10x10数组中,但是初始化数组时遇到问题。

I can initialize the first dimension of the array, but the error lies within initializing the second dimension within the first for loop. 我可以初始化数组的第一个维度,但是错误在于初始化第一个for循环内的第二个维度。

Here is what my code looks like: 这是我的代码:

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

I am getting this error: 我收到此错误:

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 ===|

You are trying to set an actual object to a pointer with the command: 您正在尝试使用以下命令将实际对象设置为指针:

map[index1] = new tile[10];

map is a tile* . maptile* However, map[index1] is a deferenced tile* making it actually a tile which cannot equal a tile* which new tile[10] gives you. 然而, map[index1]是deferenced tile*使得它实际上是一个tile不能等于一个tile*new tile[10]给你。

Therefore, your code will work better as: 因此,您的代码将在以下方面更好地工作:

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

NOTE: Technically, thats not a 2-dimensional array, is an array of arrays of structs. 注意:从技术上讲,多数民众赞成不是二维数组,而是结构数组的数组。

Said that, that note reflects the problem of your code: You have an array of arrays, so the first allocation should allocate an array which contents are pointers to other arrays : 也就是说,该注释反映了您的代码问题:您有一个数组数组,因此第一次分配应该分配一个数组,该数组的内容是指向其他数组的指针

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;

However your code could be improved if you use standard containers, like std::vector , instead of manual memory management, which is error-prone: 但是,如果您使用标准容器(例如std::vector )而不是容易出错的手动内存管理,则可以改进您的代码:

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

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

I recommend using a std::vector of shared pointers to tiles. 我建议使用共享磁贴的std::vector This will allow you to have different tiles and store them into the 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;

A nice advantage of the shared pointer is that it handles memory management (deletion) for you. 共享指针的一个好处是它可以为您处理内存管理(删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM