调试——类/对象的东西,noob问题
debuging--Class / object stuff, noob problems
请把这段代码拆开,让它变得复杂且几乎不可读,我宁愿用一次艰难的方式学习,也不愿用错误的方式多次学习相同的东西。
基类如下:
class baseMob{
private:
int _healthMax;
int _healthCurrent;
int _manaMax;
int _manaCurrent;
int _experiencePoints;
public:
//Set max Hp
void setHealthMax(int);
//Get max Hp
int getHealthMax();
//Set Current Hp
void setCurrentHealth(int);
//Get Current Health
int getCurrentHealth();
//Set Max Mana
void setMaxMana(int);
//Get Max Mana
int getMaxMana();
//Set Current Mana
void setCurrentMana(int);
//Get Current Mana
int getCurrentMana();
//getMob Exp on kill
int getExperiencePoints();
//Set mob Exp points
void setExperiencePoints(int);
//leaving out the member functions for space conservation
};
我试图创建的单个暴徒是一个绿色黏液,我试图通过我创建的默认构造函数创建它。。。
class greenSlime: private baseMob{
public:
greenSlime(){
setHealthMax(100);
setMaxMana(100);
setCurrentHealth(100);
setCurrentMana(100);
setExperiencePoints(150);
}
};
我现在的主要功能是:
greenSlime slime();
for(; slime.getCurrentHealth() >= 0; slime.setCurrentHealth(-1)){
cout << "The current health of the slime is: " << slime.getCurrentHealth() << endl;
if (slime.getCurrentHealth() <= 0 ){
cout << "Player is awarded with: " << slime.getExperiencePoints() << " Experience. ";
}
}
如果有人想把它撕了,让我看起来像个蠢驴,我真的很感激你的帮助。
我目前得到的错误是:
Project1.cpp:107:错误:请求成员getCurrentHealth' in
slime',它是非类类型"greenSlime()()"
以及其他相同类型的错误。
Tl;Dr:类实现不起作用,发布了我所有的源代码,而我可能已经发布了大约1/10,并且仍然有意义,并且希望有人告诉我为什么它不起作用以及我有多糟糕。
问题是编译器认为slime声明行是另一种类型的预声明,因为greenSlime没有带参数的ctor。
你可以通过不把括号放在黏液后面来解决它。
// greenSlime slime();
greenSlime slime;
对于你不理解的奇怪错误,以下是我能给出的最好的建议。举一个小例子说明问题,可以更容易地发现实际错误。
这是我为测试而写的。
struct Foo {
Foo() {}
void bar() {}
};
int main(int argc, char ** argv) {
Foo foo;
foo.bar();
return 0;
}
其他人指出了代码不起作用的原因。我会就程序设计提出建议。
在解释继承时,教程和编程类经常使用与代码非常相似的玩具示例。这些例子展示了什么是继承,但实际上并不能很好地展示继承的用处。
在这种情况下,我不会使用继承。我认为一个更好的设计是有一个代表暴徒类型的类,并保存该类型暴徒的所有静态数据,如暴徒名称、起始/最大生命值、攻击类型等。然后有另一个类,每个实例代表一个特定暴徒,并保存为该暴徒更改的数据,如当前生命值。
class Mob_type {
string name;
int max_hp;
vector<shared_ptr<Attack_type>> attacks;
public:
Mob_type(string name,int max_hp,vector<shared_ptr<Attack_type>> attacks)
: name(name),max_hp(max_hp),attacks(attacks) {}
int get_max_hp() const { return max_hp; }
};
class Mob {
Mob_type const &type;
int hp;
public:
Mob(Mob_type const &type) : type(type), hp(type.get_max_hp()) {}
Mob_type const &get_type() const { return type; }
int get_hp() const { return hp; }
};
void print_health(Mob const &m) {
cout << m.get_hp() << '/' << m.get_type().get_max_hp() << 'n';
}
int main() {
vector<shared_ptr<Attack_type>> attacks; // ...
Mob_type green_slime("Green Slime",50,attacks);
Mob green_slime_A(green_slime), green_slime_B(green_slime);
fight(green_slime_A,green_slime_B);
cout << "A: ";
print_health(green_slime_A);
cout << "B: ";
print_health(green_slime_B);
}
通过这种方式,您可以拥有一个包含暴徒类型的数据文件,添加类型所需做的就是更新数据文件。
class greenSlime: private baseMob{
应该是:
class greenSlime: public baseMob{
由于您从中继承的类是private,因此您看不到任何继承的方法。
此外,正如Tom Kerr所说,在声明对象之后,不需要括号。基本上,如果你不想要任何参数,在制作对象时不要使用括号。
此外,我想接下来你会遇到的事情是:你几乎从不想要私有继承,至少除非你知道你真的想要它。我猜你是想让greenSlime的类声明class greenSlime: public baseMob
首先,如果您进行私有继承,您将无法访问基类的任何函数。公共继承允许您访问基类的公共和受保护的函数和成员。
第二,如果你想制作greenSlime类的指针,你必须这样做:
//greenSlime() with parentheses
greenSlime *slime = new greenSlime();
但是,如果你想用非参数构造函数(默认构造函数)创建一个greenSlime对象,你必须这样做:
//without parentheses
greenSlime slime;
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- 简单的NOOB C 输入/输出问题:代码错误
- 调试——类/对象的东西,noob问题