私は、関数とメンバ変数の束を含む冒険者クラスを与えていました。 は、そのうちの一つは次のとおりです。C++で動的な2D配列へのポインタを初期化する方法
string*** items;
ので、最初私はそれは私が行う必要があり、3D配列だと思ったが、私はそれが2次元配列へのポインタであることを仮定と言われました。私が実行しようとしました何
は、その初期の一時配列
string** temp;
を作成し、それを埋めるだった、私は、これは、関数が終了するまで動作します
items = &temp;
一時
に私の項目をポイントします。次に、アイテム内のインデックス値を呼び出して呼び出します。何もありません。 tempが消えると配列も消えます。
このラインもdoenst仕事
(*items) = new string*[2];
私は私を助けたオンライン何かを見つけることができませんでした。
tempを使用して作成したアイテムを初期化するか、配列データを保持するにはどうしたらいいですか?コードを求める人のために
、これは彼らが私に与えたものである:
class Adventurer{
private:
string*** items;
string name;
double maxCarryWeight;
double currentCarryWeight;
int currentNumberOfItems;
int maxNumberOfItems;
double health;
static int numberOfAdventurers;
public:
Adventurer(); //default constructor
Adventurer(const Adventurer& a); //copy constructor
~Adventurer();
bool pickUpItem(string it, double weight);
bool dropItem(string it);
bool dropItem(int index);
void setName(string n);
string getName() const;
void setMaxCarryWeight(double w);
double getMaxCarryWeight() const;
void setCurrentCarryWeight(double w);
double getCurrentCarryWeight() const;
void setMaxNumberOfItems(int n);
int getMaxNumberOfItems() const;
void setCurrentNumberOfItems(int n);
int getCurrentNumberOfItems() const;
int getNumberOfAdventurers() const;
void setHealth(double h);
double getHealth() const;
string** getItem(int index) const;
Adventurer& operator = (const Adventurer& a);
};
そして
string*** items;
は、それは完全には明らかではない2D配列
「ダイナミックアレイ」と考えると、次の考えは['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)でなければなりません。 –
ベクターを使用することはできません。 –
@πάνταῥεῖIveは今では4時間のグーグル・グーグルで、何も見つからなかった。 –