gridObject という基本クラスがあります
ヘッダーファイルは次のとおりです。
#ifndef ITEM_H
#define ITEM_H
class gridObject
{
public:
gridObject();
virtual ~gridObject();
virtual int get_GridID() = 0;
virtual int get_x() = 0;
virtual int get_y() = 0;
virtual int get_direction() = 0;
void set_x(int x);
void set_y(int y);
void set_direction(unsigned int direction);
protected:
private:
int _x;
int _y;
unsigned int _direction;
};
#endif // ITEM_H
player というサブクラスがあります
クラス ファイルの get_GridID() メソッドは次のとおりです。
int player::get_GridID() { return 2; }
2D ベクトルを介してすべてのオブジェクトを追跡するグリッド クラスもあります。ヘッダー ファイルには、そのベクトルがあります。
private:
vector<vector<gridObject*> > _position;
特定の位置でオブジェクトを設定および取得するためのメソッドを次に示します。
void grid::setGridPosition(int x, int y, gridObject* obj) { _position[y][x] = obj; }
gridObject* grid::getGridPosition(int x, int y) { return _position[y][x]; }
私が抱えている問題はここにあります:
int main()
{
grid * gr = new grid(10, 10);
player p(0, 0, 100);
gridObject * go = &p;
gr->setGridPosition(0, 0, go);
cout << gr->getGridPosition(0, 0)->get_GridID();
return 0;
}
cout << gr->getGridPosition(0, 0)->get_GridID(); でクラッシュします。
適切なヘッダー ファイルをすべて含めました。
編集: グリッドのコンストラクターと _position の初期化は次のとおりです。
grid::grid(int width, int length)
{
setSize(width, length);
}
void grid::setSize(int width, int length)
{
setLength(length);
setWidth(width);
}
void grid::setLength(int val) { _position.resize(val); }
void grid::setWidth(int val)
{
for(unsigned int i = 0; i < _position.size() - 1; i++)
for(unsigned int j = 0; j < _position.at(i).size() - 1; j++)
_position.at(i).resize(val);
}