双重继承中的构造函数

Constructor in double inheritance

我的构造函数有问题。我有 class 车辆,然后我制作了 class motorVehicle 它继承了 vehicle 然后我想制作 class motorcycle 它继承了 class motorVehicle 并且我无法创建我的默认构造函数,因为我有错误: Class vehiclemotorVehicle 没有被我更改并且 class motorcycle 在这些选项的 2 个选项中 none 有效,但我给你两个都。顺便说一句,问题是(第一个选项): 'motorVehicle' 的初始化没有匹配的构造函数并且预期有第二个选项;在表达式和预期的成员名称或之后;在声明说明符之后

class vehicle {
public:
    int numberOfWheels;
    string color;
    float vehiclePayload;

    vehicle(): numberOfWheels{4},color{"black"},vehiclePayload{500}{

    }
};
class motorVehicle : public vehicle {
public:
    float tankCapacity;
        float fuelConsumption;
        float mileage;
        float currentAmountOfFuel;
        int yearOfProduction;
        unsigned long long int vin;
        motorVehicle(): tankCapacity{30}, fuelConsumption{6}, mileage{100}, currentAmountOfFuel{10}, yearOfProduction{2021}, vin{1}, vehicle{4, "black", 500} {

        }
};


class motorcycle : public motorVehicle{
public:
    float bootSize;
    string brand;
    string model;
    motorcycle(): bootSize{500}, brand{"Ninja"}, model{"Kawasaki"}, motorVehicle{30,6,100,10,2021,1,vehicle{4, "black", 500}}{

    }

};

class motorcycle : public motorVehicle{
public:
    float bootSize;
    string brand;
    string model;
    motorcycle(): bootSize{500}, brand{"Ninja"}, model{"Kawasaki"}, motorVehicle(){30,6,100,10,2021,1,vehicle{4, "black", 500}}{

    }

};

您的基 classes 没有声明任何采用实际值的构造函数。由于这些 classes do 声明了默认构造函数,聚合初始化不起作用:有一个声明的构造函数,即 class 不是聚合.不过,如果你想直接初始化基础 class,你 可以 使用它们隐式生成的复制构造函数。但是,您仍然需要单独填充 class 的内容,因为您需要使用默认构造函数创建对象:

class motorVehicle : public vehicle {
public:
    float tankCapacity;
    float fuelConsumption;
    float mileage;
    float currentAmountOfFuel;
    int yearOfProduction;
    unsigned long long int vin;
    motorVehicle(): tankCapacity{30}, fuelConsumption{6}, mileage{100}, currentAmountOfFuel{10}, yearOfProduction{2021}, vin{1},
        vehicle([]{
            vehicle rc;
            rc.numberOfWheels = 4;
            rc.color = "black";
            rc vehiclePayload = 500;
            return rc;
            }()) {

    }
 };

显然,您可以使用因子函数而不是使用 lambda 函数,假设 makeVehicle(int wheels, std::string const& color, float payload) 适当地初始化成员。不过 vehicle 确实应该有一个合适的构造函数。