我需要在main()
中创建一个对象Pokemon将它分配到PokemonWorld类中,并让PokemonWolrd决定哪个PokemonStation是这个宠物小精灵需要去的
我厌倦了分开获取数据(得到名字和hp)并聚在一起(得到一个口袋妖怪类)
但都失败了
#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");
};
答案 0 :(得分:0)
在C ++中,您应该将std::vector
s用于动态事物列表,并使用std::string
s用于文本。如果你了解Java,那就像ArrayList
和String
。 (要使用这些,请确保#include
<vector>
和<string>
。)
例如,您的Pokemon
课程已被name
重写为string
:
class Pokemon {
public:
Pokemon() {}
Pokemon(string name, int hp):name(name), hp(hp) { // construct the fields directly
}
private:
string name;
int hp;
};
您的PokemonStation
课程,使用vector
为口袋妖怪列表重写:
class PokemonStation {
private:
vector<Pokemon> list;
public:
PokemonStation() {}
PokemonStation(int x):id(x) {}
int id;
void assignToList(Pokemon x)
{
list.push_back(x); // add x to the list
}
};
如果您要使用Pokemon
打印cout <<
,那么您必须重载<<
运算符以定义要打印的内容。将其添加到您的班级中:
class Pokemon {
public:
...
friend ostream& operator<<(ostream& out, const Pokemon& p) {
out << p.name; // just print the name
return out;
}
...
};
只需确保您cout
Pokemon
Pokemon
,而不是指向Pokemon*
(WHERE ....
AND CONTAINS( jsondata, 'NEAR(('Payments,460),1)')
),然后您就赢了。得到一个地址。