正在从另一个 class 初始化 C++ class 属性

Initializing C++ class attribute from another class

目前,我正在尝试实现我目前所学的内容:C++ 中的 OOP class。这里我有两个不同的 classes:VehicleInfo 和 Vehicle。下面是我写的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#define M_PI  3.1416
using namespace std;

class VehicleInfo{
    public:
        string brand;
        bool electric;
        int catalogue_price;
        float tax_percentage = 0.05;

        VehicleInfo(string brand, bool electric, int catalogue_price){
            VehicleInfo::brand = brand;
            VehicleInfo::electric = electric;
            VehicleInfo::catalogue_price = catalogue_price;
        }

        float compute_tax(){
            if (VehicleInfo::electric == true){
                tax_percentage = 0.02;
                }
            return VehicleInfo::catalogue_price * tax_percentage;
        }

        void print_vehicle_info(){
            std::cout << "Brand : " << brand << std::endl;
            std::cout << "Payable Tax : " << compute_tax() << std::endl;
        }
};

class Vehicle{
    public:
        string id;
        string license_plate;
        VehicleInfo vehicle;
        
        Vehicle(string id, string license_plate, VehicleInfo vehicle){
            Vehicle::id = id;
            Vehicle::license_plate = license_plate;
            Vehicle::vehicle = vehicle;
        }

        string getName(){
            return Vehicle::vehicle.brand;
        }

        int getTax(){
            return Vehicle::vehicle.compute_tax();
        }

        void print_vehicle(){
            std::cout << "ID : " << id << std::endl;
            std::cout << "License Plate : " << license_plate << std::endl;
            std::cout << "Brand : " << getName() << std::endl;
            std::cout << "Tax : " << getTax() << std::endl;
        }

};


int main(int argc, char const *argv[])
{
    cout << endl;
    VehicleInfo unit1 = VehicleInfo("Tesla Model 3", true, 60000);
    unit1.print_vehicle_info();

    Vehicle unit2 = Vehicle("YU2314", "KL0932", unit1);
    unit2.print_vehicle();

    std::cin.get();
    return 0;
}


我对代码的期望是 class Vehicle 中的属性 'vehicle' 将使用 class VehicleInfo 中的属性和函数 'compute_tax()' 进行初始化。我收到的错误代码显示如下:

cohesion_coupling.cpp: In constructor 'Vehicle::Vehicle(std::__cxx11::string, std::__cxx11::string, VehicleInfo)':
cohesion_coupling.cpp:42:70: error: no matching function for call to 'VehicleInfo::VehicleInfo()'
         Vehicle(string id, string license_plate, VehicleInfo vehicle){
                                                                      ^
cohesion_coupling.cpp:17:9: note: candidate: 'VehicleInfo::VehicleInfo(std::__cxx11::string, bool, int)'
         VehicleInfo(string brand, bool electric, int catalogue_price){
         ^~~~~~~~~~~
cohesion_coupling.cpp:17:9: note:   candidate expects 3 arguments, 0 provided
cohesion_coupling.cpp:10:7: note: candidate: 'VehicleInfo::VehicleInfo(const VehicleInfo&)'
 class VehicleInfo{
       ^~~~~~~~~~~
cohesion_coupling.cpp:10:7: note:   candidate expects 1 argument, 0 provided
cohesion_coupling.cpp:10:7: note: candidate: 'VehicleInfo::VehicleInfo(VehicleInfo&&)'
cohesion_coupling.cpp:10:7: note:   candidate expects 1 argument, 0 provided

任何改进代码的建议(或者可能需要对我所做的 class 实践进行一些更正)?

在您的 Vehicle 构造函数中,编译器首先必须默认构造所有成员变量。只有在那之后,构造函数 运行 和 Vehicle::vehicle = vehicle; 中的代码才会被调用。 所以你的vehicle成员变量会先默认构造然后赋值。

但是您的 VehicleInfo 没有默认构造函数,这是您遇到的错误。

要解决这个问题,请像这样使用 member initializers

Vehicle(string id, string license_plate, VehicleInfo vehicle) :
    id(id),
    license_plate(license_plate),
    vehicle(vehicle)
{
}