学校から受け取った課題の一部として、この割り当て/破棄の問題をかなりの時間解決しようとしてきました。基本的に、設計した単純なクラスの 2 次元配列ポインターを割り当てる必要があります。
class RobotsWorld{
public:
RobotsWorld(int width,int hight);
~RobotsWorld();
/**
* copy,Assign,operator=
*/
//RobotsWorld(const RobotsWorld &);
//void Assign(const RobotsWorld &);
//RobotsWorld& operator=(const RobotsWorld &);
bool addRobot(Robot & r,Point p);
bool moveRobot(Robot & r);
bool removeRobot(Robot & r);
Point getPosition(Robot r);
string toString();
Robot* getRobotInWorld(Robot & r);
/** validation functions **/
bool checkOutOfBounds(int,int);
bool isEmptySlot(int x,int y);
bool ableToMove(Robot &);
private:
Robot*** robots;
Point point;
int width,height;
};
これは関数の完全なソース ファイルではありませんが、これらの関数はクラッシュ\メモリの損失を引き起こします。(そして、はい、トリプル ポインターとして定義する必要があります - ***ロボット)
RobotsWorld::RobotsWorld(int width,int height)
:width(width),height(height)
{
robots = new Robot**;
*robots = new Robot*[height];
for(int i = 0 ; i < height ; i++){
robots[i] = new Robot*[width];
for(int j = 0 ; j < width ; j++)
robots[i][j] = NULL;
}
}
RobotsWorld::~RobotsWorld(){
for(int i = 0 ; i < height ; i++){
delete [] robots[i];
robots[i] = NULL;
}
delete [] *robots;
delete **robots;
}
ロボットクラスとメインは割り当てられませんでした。私は解決策を探していましたが、この状況を説明することさえ困難であることがわかりました.