Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多级继承/多态与虚函数_C++_Function_Inheritance_Polymorphism_Virtual - Fatal编程技术网

C++ 多级继承/多态与虚函数

C++ 多级继承/多态与虚函数,c++,function,inheritance,polymorphism,virtual,C++,Function,Inheritance,Polymorphism,Virtual,我有一个多级继承(来自Ship类->MedicShip类->Medic类),虚拟函数代码如下。我想结果应该是: 医生10 医生10 但它产生了奇怪的结果。另一方面,如果我只使用一级继承(从Ship类->Medic类,中间没有MedicShip类),结果就可以了。你能找出我的错误吗?多谢 #ifndef FLEET_H #define FLEET_H #include <string> #include <vector> using namespace std; cla

我有一个多级继承(来自Ship类->MedicShip类->Medic类),虚拟函数代码如下。我想结果应该是:

医生10
医生10

但它产生了奇怪的结果。另一方面,如果我只使用一级继承(从Ship类->Medic类,中间没有MedicShip类),结果就可以了。你能找出我的错误吗?多谢

#ifndef FLEET_H
#define FLEET_H
#include <string>
#include <vector>

using namespace std;

class Ship
{
    public:
        Ship(){};
        ~Ship(){};
        int weight;
        string typeName;

        int getWeight() const;
        virtual string getTypeName() const = 0;
};

class MedicShip: public Ship
{
    public:
        MedicShip(){};
        ~MedicShip(){};
        string getTypeName() const;
};

class Medic: public MedicShip
{
    public:
        Medic();
};

class Fleet
{
    public:
        Fleet(){};
        vector<Ship*> ships;
        vector<Ship*> shipList() const;
};
#endif // FLEET_H



#include "Fleet.h"
#include <iostream>

using namespace std;

vector<Ship*> Fleet::shipList() const
{
    return ships;
}

int Ship::getWeight() const
{
    return weight;
}

string Ship::getTypeName() const
{
    return typeName;
}

string MedicShip::getTypeName() const
{
    return typeName;
}

Medic::Medic()
{    
    weight = 10;    
    typeName = "Medic";
}

int main()
{
    Fleet fleet;
    MedicShip newMedic;

    fleet.ships.push_back(&newMedic);
    fleet.ships.push_back(&newMedic);

    for (int j=0; j< fleet.shipList().size(); ++j)
    {
        Ship* s =  fleet.shipList().at(j);
        cout << s->getTypeName() << "\t" << s->getWeight() << endl;
    }

    cin.get();
    return 0;
}
#如果没有车队#
#定义舰队
#包括
#包括
使用名称空间std;
班轮
{
公众:
船舶{};
~Ship(){};
整数权重;
字符串类型名;
int getWeight()常量;
虚拟字符串getTypeName()常量=0;
};
医疗船类别:公共船
{
公众:
MedicShip(){};
~MedicShip(){};
字符串getTypeName()常量;
};
医疗班:公立医疗队
{
公众:
医生();
};
级船队
{
公众:
舰队{};
矢量船;
向量shipList()常量;
};
#endif//FLEET\u H
#包括“Fleet.h”
#包括
使用名称空间std;
vector Fleet::shipList()常量
{
返回船舶;
}
int Ship::getWeight()常量
{
返回重量;
}
字符串Ship::getTypeName()常量
{
返回typeName;
}
字符串MedicShip::getTypeName()常量
{
返回typeName;
}
Medic::Medic()
{    
重量=10;
typeName=“Medic”;
}
int main()
{
船队;
新医疗补助;
舰队。船只。推回(&newMedic);
舰队。船只。推回(&newMedic);
对于(int j=0;j第一个错误就在这里。如果您想通过基类指针删除派生类对象,则此析构函数应该是虚拟的。

您还没有创建任何类
Medic
的实例。您是说

Medic newMedic;
而不是

MedicShip newMedic;

也许?因此,
Medic
构造函数没有被调用,
weight
typeName
也没有被初始化。

这很正确,很重要,但damar报告的问题是无关的。(在这种情况下,错误不会产生负面影响,因为派生类在销毁时不会也不必做任何与基类不同的事情。)是的,你是对的。我错过了。非常感谢纳瓦兹。第二个答案中的加雷思·麦考汉也发现了我的错误。它应该是Medic newMedic而不是MedicShip newMedic。它现在工作了。是的,你是对的加雷思。我的错误是从我以前的代码中复制过来的。非常感谢你。它现在工作了……也许结果太奇怪了,无法用r来解释我在这里见过。
MedicShip newMedic;