-4
我需要在main()我如何分配類對象爲類
是分配給它的進級PokemonWorld以創造一個對象寵物小精靈,讓PokemonWolrd決定哪些PokemonStation這是口袋妖怪需要去
我累了獲取數據separatly(取姓名和馬力)和聚(獲得寵物小精靈類)
但都失敗
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class Pokemon {
public:
Pokemon() {};
Pokemon(char x[], int n) {
strncpy_s(name, x, 10);
hp = n;
};
private:
char name[10];
int hp;
};
class PokemonStation {
private:
Pokemon **list= new Pokemon*[1000];
public:
PokemonStation() {};
PokemonStation(int x) {
id = x;
};
int id;
void assigntoList(int i,Pokemon x)
{
if (i > 0)
i--;
list[i] = new Pokemon(x);
cout << "creat" << list[i];
};
};
class PokemonWorld {
private:
char name[10];
public:
PokemonStation s1;
PokemonStation s2;
PokemonWorld() {};
PokemonWorld(char x[], int y=1, int z=2) {
strncpy_s(name, x, 10);
PokemonStation s1(y);
PokemonStation s2(z);
};
const char* const getName() const{
return name;
};
void assigntoStation(int i,Pokemon x) {
if (i == 0 || i % 2 == 0)
s1.assigntoList(i, x);
else
s2.assigntoList(i, x);
};
};
void main() {
int number,hp,i;
char name[10];
cout << "What is the World Name ?" <<endl;
cin >> name;
PokemonWorld world(name);
cout << "Please input the number of Pokemon in the " << world.getName() <<" world:" << endl;
cin >> number;
Pokemon **mon = new Pokemon*[number];
cout << "Please input the characteristics of all Pokemon: Name HP" << endl;
for (i = 0;i < number;i++)
{
cin >> name >> hp;
mon[i] = new Pokemon(name, hp);
world.assigntoStation(i,*(mon[i]));
}
for (i = 0;i < number;i++)
cout << "world is " << world.getName() << endl;
system("pause");
};
如果你想在C++中使用動態列表,你應該使用['std :: vector'](http://www.cplusplus.com/reference/vector/vector/)而不是原始數組。也可以使用['std :: string's](http://www.cplusplus.com/reference/string/string/)作爲文本。 – qxz
你的程序究竟發生了什麼不工作? – qxz
當我嘗試打印出PokemonStation中的函數assigntoList中的小寵物來測試我是否成功地將口袋妖怪分配到列表[i]中時,它打印出列表[i] –