C++ 动态分配2dint数组

C++ 动态分配2dint数组,c++,arrays,C++,Arrays,有人能指出我在下面的代码中做错了什么吗 int* a = NULL; int* b = NULL; a = new int[map->mapSize.width]; b = new int[map->mapSize.height]; layer->tileGids = new int[a][b]; 下面是代码使用的内容: typedef struct _size { int width, height; } size; class Map {

有人能指出我在下面的代码中做错了什么吗

int* a = NULL;   
int* b = NULL;   
a = new int[map->mapSize.width];
b = new int[map->mapSize.height];

layer->tileGids = new int[a][b];
下面是代码使用的内容:

typedef struct _size {
    int width, height;
} size;

class Map {
    size mapSize;
}

class Layer {
    int * tileGids;
}
编辑:编译器错误(在第一位代码的第6行):

  • 错误:新声明符中的表达式必须具有整型或枚举类型|
  • 错误:“b”不能出现在常量表达式中|
解决方案:

class Layer {
int ** tileGids; // NOTE the "**" to indicate tileGids is a pointer to pointer i.e. 2D array.
}

我决定接受光之炼金术士的回答。本质上,对我有效的是使用向量而不是数组。Vector为您管理内存,因此更容易处理。

a和b是
int*
这里:

layer->tileGids = new int[a][b];
也许你是故意这么说的

layer->tileGids = new int[*a][*b];

a和b在这里是
int*

layer->tileGids = new int[a][b];
也许你是故意这么说的

layer->tileGids = new int[*a][*b];
有人能指出我在下面的代码中做错了什么吗

int* a = NULL;   
int* b = NULL;   
a = new int[map->mapSize.width];
b = new int[map->mapSize.height];

layer->tileGids = new int[a][b];
很多。您正在分配两个单独的数组(一个“行数组”和一个“列数组”,这不是您需要的),然后您尝试做一些奇怪的事情


一般来说,你不能(严格地说)在C++中动态分配一个2D数组(因为类型系统仍然需要在编译时知道的类型以及维数)。您可以用一组阵列来模拟它,但最好的方法是分配一个1D阵列:

int width=5;

std::vector<int> tab(width*height);
通过这种方式,您实际上将一维数组解释为二维数组(性能等同于静态二维数组)

然后,为了方便起见,最好用类包装索引<代码>boost::multi_array也已经为您完成了这项工作

有人能指出我在下面的代码中做错了什么吗

int* a = NULL;   
int* b = NULL;   
a = new int[map->mapSize.width];
b = new int[map->mapSize.height];

layer->tileGids = new int[a][b];
很多。您正在分配两个单独的数组(一个“行数组”和一个“列数组”,这不是您需要的),然后您尝试做一些奇怪的事情


一般来说,你不能(严格地说)在C++中动态分配一个2D数组(因为类型系统仍然需要在编译时知道的类型以及维数)。您可以用一组阵列来模拟它,但最好的方法是分配一个1D阵列:

int width=5;

std::vector<int> tab(width*height);
通过这种方式,您实际上将一维数组解释为二维数组(性能等同于静态二维数组)


然后,为了方便起见,最好用类包装索引<代码>boost::multi_array也已经为您完成了此操作。

您无法传递初始化数组大小的指针。其他人现在提到了这一点

这篇文章(不是我的)似乎可以帮助你:


您还应该考虑在类层的构造函数中进行分配,然后删除析构函数中的内存(即RAI-资源获取是初始化)。这被认为是好的风格

<>最后,您可以考虑使用连续内存和自定义索引方案,您可以很容易地使用该层来封装。这当然取决于事情会变得多大。它们越大,连续内存的情况就越好

这应该给你一点味道

#include <iostream>
#include <cstdlib>

int main()
{
   const size_t ROWS = 5; 
   const size_t COLS = 2; 
   const size_t size = ROWS*COLS;

   int* arr = new int[size];

   int i = 0;
   for ( size_t r = 0 ; r < ROWS; ++r )
   {
      for (size_t c = 0; c < COLS; ++c )
      {
         arr[r*COLS+c] = i++;
      }
   }

   for ( int j = 0; j < i; ++j)
   {
      std::cout << arr[j] << std::endl;
   }

   delete [] arr;
}
#包括
#包括
int main()
{
const size_t ROWS=5;
常数大小=2;
常量大小=行*列;
int*arr=新的int[size];
int i=0;
对于(大小r=0;rstd::cout不能传递用于初始化数组大小的指针。其他人现在已经提到了这一点

这篇文章(不是我的)似乎可以帮助你:


您还应该考虑在类层的构造函数中进行分配,然后删除析构函数中的内存(即RAI-资源获取是初始化)。这被认为是好的样式。

最后,您可以考虑使用连续内存和自定义索引方案,您可以很容易地使用该层来封装。这当然取决于大的事物将如何获得。它们越大,连续内存的情况就越好。

这应该给你一点味道

#include <iostream>
#include <cstdlib>

int main()
{
   const size_t ROWS = 5; 
   const size_t COLS = 2; 
   const size_t size = ROWS*COLS;

   int* arr = new int[size];

   int i = 0;
   for ( size_t r = 0 ; r < ROWS; ++r )
   {
      for (size_t c = 0; c < COLS; ++c )
      {
         arr[r*COLS+c] = i++;
      }
   }

   for ( int j = 0; j < i; ++j)
   {
      std::cout << arr[j] << std::endl;
   }

   delete [] arr;
}
#包括
#包括
int main()
{
const size_t ROWS=5;
常数大小=2;
常量大小=行*列;
int*arr=新的int[size];
int i=0;
对于(大小r=0;rtileGids=newint[a][b]
是问题的根本原因

我试图猜测您的意图,我认为您试图做的是使layer.tileGids成为一个二维数组,以引用大小为(mapSize.Width、mapSize.height)的“网格”,这样您就可以使用layer.tileGids[x][y]引用网格中的每个“单元”

如果您确实在尝试创建二维数组,那么有两种方法可以做到这一点

方法1:

class Layer {
int ** tileGids; // NOTE the "**" to indicate tileGids is a pointer to pointer i.e. 2D array.
}
要初始化它,请执行以下操作:

int width = map->mapSize.width;
int height = map->mapSize.height;
layer.tileGids = new int*[width]; // NOTE the "int*" to indicate tileGids is a new array of pointers to int.
for (int i = 0; i < width; i++) // Initialize each element in layer.tileGids[] to be a pointer to int.
{
  layer.tileGids[i] = new int[height];
}
int-width=map->mapSize.width;
int height=map->mapSize.height;
layer.tileGids=new int*[width];//注意,表示tileGids的“int*”是指向int的指针的新数组。
for(int i=0;i
现在,您可以使用以下方式访问layer.tileGids中的项目:

int value = layer.tileGids[x][y] // where 0 <= x < width and 0 <= y < height
int value=layer.tileGids[x][y]//其中0 mapSize.width;
int height=map->mapSize.height;
layer.tileGids=vector(宽度,向量(高度,0));//将所有条目初始化为0。
要访问元素,请执行以下操作:

int value = layer.tileGids[x][y]; // Where 0 <= x < width and 0 <= y < height
int value=layer.tileGids[x][y];//其中0首先,变量“a”和“b”是指针。代码:
图层->tileGids=newint[a][b]
是问题的根本原因

我试图猜测您的意图,我认为您试图做的是使layer.tileGids成为一个二维数组,以引用大小为(mapSize.Width、mapSize.height)的“网格”,这样您就可以