Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+;中将二维指针数组初始化为成员变量+;_C++_Class_Pointers_Dynamic Arrays - Fatal编程技术网

C++ 在C+;中将二维指针数组初始化为成员变量+;

C++ 在C+;中将二维指针数组初始化为成员变量+;,c++,class,pointers,dynamic-arrays,C++,Class,Pointers,Dynamic Arrays,我一直在Stack Overflow和cplusplus论坛周围搜索,但运气不好。我是C++新手,目前正在为一个学校项目做游戏。我在创建、填充、销毁2d指针数组方面没有问题,并且在我的游戏中使用了一个指针数组,效果非常好 我为一个玩家创建了一个新类,并将2d数组移动到该类中。现在,当我试图运行我的程序时,我遇到了一个分段错误。谁能告诉我为什么会这样?bold我不能简单地使用std::vector,因为指针的2d数组是必需的 功能 short** Player::allocate_2d_array

我一直在Stack Overflow和cplusplus论坛周围搜索,但运气不好。我是C++新手,目前正在为一个学校项目做游戏。我在创建、填充、销毁2d指针数组方面没有问题,并且在我的游戏中使用了一个指针数组,效果非常好

我为一个玩家创建了一个新类,并将2d数组移动到该类中。现在,当我试图运行我的程序时,我遇到了一个分段错误。谁能告诉我为什么会这样?bold我不能简单地使用std::vector,因为指针的2d数组是必需的

功能

short** Player::allocate_2d_array()
{
  short **array2d;

  // Insert columns
  for(int i = 0; i < this->BOARD_DIMENSIONS; i++)
  {
    array2d[i] = new short[this->BOARD_DIMENSIONS];
    // Fill in the numbers
    for(int j = 0; j < this->BOARD_DIMENSIONS; j++)
    {
      array2d[i][j] = 0;
    }
  }

  return array2d;
}

for循环正文的第一行在为指针
阵列2d
(使用方括号运算符)分配内存或初始化它之前取消对它的引用

为了避免这个问题,您必须在进入for循环之前分配数组的第一个维度。一旦分配了这个指针数组,就可以分配
BOARD\u维度
数组的
short
s,并将指向它们的指针存储在第一个数组的元素中

比如:

short** array2d = new short*[BOARD_DIMENSIONS];
for (size_t u = 0; u < BOARD_DIMENSIONS; ++u)
{
    array2d[u] = new short[BOARD_DIMENSIONS];
    // You could then use memset to initialize the array, or
    // use a for loop, like in your example:
    for (size_t v = 0; v < BOARD_DIMENSIONS; ++v)
    {
        array2d[u][v] = 0;
    }
 }
short**array2d=新的short*[BOARD_DIMENSIONS];
用于(尺寸=0;u<电路板尺寸;++u)
{
array2d[u]=新的短板[BOARD_维度];
//然后可以使用memset初始化数组,或者
//使用for循环,如示例中所示:
用于(尺寸v=0;v<电路板尺寸;++v)
{
阵列2d[u][v]=0;
}
}

确保在使用完内存后正确释放内存,使用
操作符delete[]

可以使用:vectorboard(board_维度,vector(board_维度,0));若要替换allocate_2d_array,我不确定您为什么希望
allocate_2d_array
可公开访问它不应公开,复制粘贴错误。Argh!教科书业余爱好者的错误。。。谢谢嗯。正如其他人所指出的,如果这不是家庭作业,那么使用一个或两个STL数据容器可能会更好。因此,除非我创建指向Person类的指针,否则它是有效的。例如某个人或某个花花公子;很好,但是。Person somedude=新人;仍然会生成分段错误。
Person somedude
Person
的一个实例;你需要一个指针。尝试
Person*pSomedude=newperson
。这就是我所拥有的,它可以编译,但当我尝试使用上述函数时,会导致分段错误。
Player::Player()
{
  this->turns_taken = 0;
  // Prepare the board
  this->board = this->allocate_2d_array();
  ...
}
short** array2d = new short*[BOARD_DIMENSIONS];
for (size_t u = 0; u < BOARD_DIMENSIONS; ++u)
{
    array2d[u] = new short[BOARD_DIMENSIONS];
    // You could then use memset to initialize the array, or
    // use a for loop, like in your example:
    for (size_t v = 0; v < BOARD_DIMENSIONS; ++v)
    {
        array2d[u][v] = 0;
    }
 }