c++派生类构造函数问题
C++ derived class constructor issue
我的构造函数有一些问题,因为目前只有基本构造函数由于某种原因被使用,我的多参数构造函数没有被使用。生物是我的抽象祖父类,而战士来自生物,地精来自战士
combatant fighter_1;
combatant fighter_2;
do
{
cout << "Please pick the first combatant, enter 1 for a Goblin, n";
cout << "enter 2 for a Barbarian, enter 3 for a Reptile person, n" ;
cout << "enter 4 for a Blue Men, enter 5 for a Beserker, n";
cout << "enter 6 for a hobbit. n";
cin >> pick;
checker=pick;
if (cin.fail()||checker!=pick||pick<1||pick>6)
{
cout << "You have entered an invalid input.n";
cin.clear();
cin.ignore(100, 'n');
pick=-1;
}
}
while (checker!=pick||pick<1||pick>6); //makes sure number entered is valid
if (pick==1)
{
goblin fighter_1("Goblin",6,6,0,6,0,0,3,8);
}
我为每个生物类都有一个构造函数,但每个构造函数的作用都与地精相同
creature::creature(): chartype("no name yet"), adice1(0),
adice2(0), adice3(0),ddice1(0), ddice2(0), ddice3(0), defense(0),health(0)
{
}
combatant :: combatant(): creature()
{
}
creature::creature( string name, int attack1, int attack2, int attack3, int def1, int def2,
int def3, int defense, int health): chartype(name), adice1(attack1), adice2(attack2),
adice3(attack3),ddice1(def1), ddice2(def2), ddice3(def3), defense(defense),health(health)
{
}
combatant::combatant(string name, int attack1, int attack2, int attack3, int def1, int def2,
int def3, int defense, int health) :creature(name, attack1, attack2, attack3, def1, def2,
def3, defense, health)
{
}
goblin::goblin(string name, int attack1, int attack2, int attack3, int def1, int def2,
int def3, int defense, int health) :combatant(name, attack1, attack2, attack3, def1, def2,
def3, defense, health)
{
}
通常,当if
主体中的单个语句没有执行时(正如您报告的情况),这是由于if
条件不是true
。
我从几个月前拿起这段代码,意识到我没有使用最新的版本,我应该把它作为
fighter_1 = goblin("Goblin",6,6,0,6,0,0,3,8);
相关文章:
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- constexpr构造函数需要常量成员函数时出现问题
- 有关 c++ 构造函数的问题
- 关于复制构造函数的一个棘手问题
- 复制构造函数隐式转换问题
- 构造函数 (C++) 中的 char 指针参数存在问题
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 关于 std::bitset 构造函数的几个问题?
- 构造函数和 G++ 编译配方的问题
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- C++ 关于两个类构造函数的问题
- 在构造函数 c++ 中初始化属性时出现问题
- 空构造函数和值传递问题
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 复制和赋值构造函数的问题
- 变分模板递归构造函数问题
- C++中用户定义的构造函数出现问题
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- C++的抽象类继承和构造函数的问题
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?