0

所以我试图创建一个指向 Piece 类型对象的二维指针数组。问题是当我尝试将指针分配给数组时,我得到了分段错误。我意识到我需要在开始分配之前将数组初始化到某个时间,但我做不到。

这是 Map 的头文件,其中包含一个二维指针数组。

#ifndef MAP_H
 #define MAP_H

 #include <iostream>
 #include <vector>
 #include <fstream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sstream>
 #include <string>
 #include <cstring>
 #include "Player.h"
 #include "Sprite.h"
 #include "Piece.h"
 #include "Messages.h"
 #include "PieceType.h" 

using namespace std;

class Map
{
    private:

        Piece*** pieces;
        int startingX;
        int startingY;
        int width;
        int height;
        string mapName;

    public:

        Map(string);
        ~Map();

        void printMap() const;
        Piece* pieceType(char);
        void setSprite(Piece*);
        void firstMove();
        void resetMap(string);

        bool moveUp(int, int);
        bool moveDown(int, int);
        bool moveLeft(int, int);
        bool moveRight(int, int);

        int getHeight();
        int getWidth();


};

#endif

我正在谈论的数组是碎片。

我尝试在 Map 的构造函数中分配它。

Map::Map(string name)
{
  ifstream map;
  string line;
  string dimention;
  mapName = name;

  map.open(name.c_str());

  if (map.good())
  {
    getline (map, line);

    int i = 0;

    while(line[i] != 'X')
    {
      dimention[i] = line[i];
      i++;
    }

    stringstream convert(dimention);

    convert >> width;

    int temp = i;
    dimention = "";
    i = 1;

    while(line[(i + temp)] != '\0')
    {
      dimention[i] = line[(i + temp)];
      i++;
    }

    stringstream convertTwo(dimention);

    convertTwo >> height;

    for (int i = 0; i < height; i++)
     {
       if (!(map.eof()))
       { 
     getline (map, line);
       }
       else
       {
     cout << "Error with file" << endl;
     break;
       }

       for (int j = 0; j < width; j++)
       {
     pieces[i][j] = pieceType(line[j]); //This is where I'm getting the segmentation fault

     cout << "assigned" << endl;

     if ((pieces[i][j])->getType() == WAYPOINT)
     {

       if (pieces[i][j]->getWaypointType() == 0)
       {
         startingX = j;
         startingY = i;
       }
     }

     else
     {       
     (pieces[i][j])->setXCordinate(j);
     (pieces[i][j])->setYCordinate(i);
     }

       }
     }
  }
}

其中 name 是一个字符串,其中包含具有用于加载特定地图的信息的文件的名称。

函数pieceType也如下:

Piece* Map::pieceType(char type)
{
  Piece* temp;

  if (type == '.')
  {
    return NULL;
  }
  if (type == 'S')
  {
    temp = new Waypoint(0);
    return temp;
  }
  if (type == 'E')
  {
    temp = new Waypoint(1);
    return temp;
  }
}

Waypoint 是 Piece 的派生类。

4

2 回答 2

2

问题确实是您必须初始化该数组。像这样:

pieces=new Piece**[height];
for(int i=0;i<height;i++){
     pieces[i]=new Piece*[width];
}

在你得到widthand之后height,在你开始使用之前写下pieces。但是您应该知道:对于每个new,都应该有一个对应的delete,否则该内存将永远不会被释放,并且您将遇到内存泄漏。要释放该内存,请将其添加到您的析构函数中:

for(int i=0;i<height;i++){
    for (int j = 0; j < width; j++){
        delete pieces[i][j];
    }
    delete[] pieces[i];
}
delete[] pieces;

这假设 eachpieces[i][j]包含分配的对象new或 NULL,并且它适用于两者。查看您的代码,这似乎是您的情况。但是,如果没有分配其中之一(不是您的情况),它将不起作用。

于 2013-10-06T14:39:06.940 回答
0

使用std::vector<std::vector<Pieces>>而不是(尝试,因为它不起作用)重新发明轮子。它安全、简单,并且避免了手动内存管理的麻烦。

于 2013-10-06T14:47:14.927 回答