c++派生类构造函数问题

C++ derived class constructor issue

本文关键字:问题 构造函数 派生 c++      更新时间:2023-10-16

我的构造函数有一些问题,因为目前只有基本构造函数由于某种原因被使用,我的多参数构造函数没有被使用。生物是我的抽象祖父类,而战士来自生物,地精来自战士

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);