You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++编译错误:无法匹配Order::Order()构造函数调用

问题解决:C++构造函数调用错误

问题描述

编写C++代码时遇到编译错误,在Passenger类构造函数中传入Order对象指针触发错误,但OrderFlight类的addPassenger方法中类似操作未报错,需解决该问题。

代码示例

#include <iostream>

using namespace std;

class Customer
{
    public :
    
    string email, name, address;
    long telephoneNo;
    
    int addNew()
    {
        cout<<"Enter the email, name, address and telephone no. of the user Please.\n";
        cin>>email>>name>>address>>telephoneNo;
        return 0;
    }
    
    int edit()
    {
        cout<<"Enter new name, email, address and telephone no. of user Please.\n";
        cin>>email>>name>>address>>telephoneNo;
        return 0;
    }
    
    int update()
    {
        cout<<"Enter new email, address and telephone no. of user Mr./Ms. "<<name<<" Please.\n";
        cin>>email>>address>>telephoneNo;
        return 0;
    }
    
};

class Order : public Customer
{
    public :
    
    static int number;
    const float price = 10000.0;
    float payment;
    string state, billingAddress;
    bool created, paymentMade;
    Customer *customer;
    
    Order(Customer *customer)
    {
        number++;
        this->customer = customer;
        payment = price + (0.1f*price);
        state = "West Bengal";
        billingAddress = customer->address;
        created = true;
        paymentMade = false;
    }
    
    int setState(string state)
    {
        this->state = state;
        return 0;
    }
    
    float getPrice()
    {
        return price;
    }
    
};
int Order::number = 0;

class Passenger : public Order
{
    public :
    
    string name;
    bool insurance, priorityBoarding;
    float luggage, extraLuggage;
    Order *order;
    
    Passenger(Order *order)
    {
        char flag;
        this->order = order;
        name = order->customer->name;
        cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
        cin>>luggage;
        if(luggage > 15.0)
            extraLuggage = luggage - 15.0f;
        cout<<"Is the customer eligible for insurance. y/n? \n";
        cin>>flag;
        if(flag == 'y')
            insurance = true;
        else
            insurance = false;
        cout<<"Is the customer a priority boarder. y/n? \n";
        cin>>flag;
        if(flag == 'y')
            priorityBoarding = true;
        else
            priorityBoarding = false;
    }
    
    Passenger get()
    {
        return *this;
    }
    
    int edit()
    {
        char flag;
        cout<<"Please re-enter the name of the Passenger.\n";
        cin>>name;
        cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
        cin>>luggage;
        if(luggage > 15.0)
            extraLuggage = luggage - 15.0f;
        cout<<"Is the customer eligible for insurance. y/n? \n";
        cin>>flag;
        if(flag == 'y')
            insurance = true;
        else
            insurance = false;
        cout<<"Is the customer a priority boarder. y/n? \n";
        cin>>flag;
        if(flag == 'y')
            priorityBoarding = true;
        else
            priorityBoarding = false;
        return 0;
    }
    
    int update()
    {
        char flag;
        cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
        cin>>luggage;
        if(luggage > 15.0)
            extraLuggage = luggage - 15.0f;
        cout<<"Is the customer eligible for insurance. y/n? \n";
        cin>>flag;
        if(flag == 'y')
            insurance = true;
        else
            insurance = false;
        cout<<"Is the customer a priority boarder. y/n? \n";
        cin>>flag;
        if(flag == 'y')
            priorityBoarding = true;
        else
            priorityBoarding = false;
        return 0;
    }

};

class OrderFlight : public Order, public Passenger
{
    public :
    
    static int flights;
    Passenger* passenger;
    
    int addPassenger(Order *order)
    {
        Passenger* passenger = new Passenger(&order);
        this->passenger = passenger;
        return 0;
    }
    
    int removePassanger()
    {
        delete(&passenger);
        return 0;
    }
    
    /*
    float getPrice(Passenger *passenger)
    {
        Passenger* gPassenger = Passenger::get();
        
    }
    */
};
int OrderFlight::flights = 50;

int main()
{
    cout<<"Hello World";

    return 0;
}

编译错误信息

In constructor ‘Passenger::Passenger(Order*)’: main.cpp:84:5: error: no matching function for call to ‘Order::Order()’


错误原因

Passenger类继承自Order类,但Order类仅定义了带Customer*参数的构造函数,没有默认无参构造函数。创建Passenger对象时,编译器会自动尝试调用父类Order的默认构造函数初始化基类部分,由于Order不存在该构造函数,因此触发编译错误。

另外,OrderFlight类的addPassenger方法看似无报错,实际存在参数类型错误(传递了Order**而非Order*),只是当前编译流程未走到这一步。

解决方法

1. 修正类继承逻辑(核心)

Passenger无需继承Order,业务逻辑上Passenger是关联一个Order,而非成为Order的子类。去掉继承关系,使用组合(成员变量关联)更符合面向对象设计原则,同时解决构造函数问题:

// 修改后的Passenger类定义
class Passenger
{
    public :
    
    string name;
    bool insurance, priorityBoarding;
    float luggage, extraLuggage;
    Order *order;
    
    Passenger(Order *order)
    {
        char flag;
        this->order = order;
        name = order->customer->name;
        cout<<"Enter the luggage amount for the customer Mr./Ms. "<<name<<", in kgs Please.\n";
        cin>>luggage;
        if(luggage > 15.0)
            extraLuggage = luggage - 15.0f;
        cout<<"Is the customer eligible for insurance. y/n? \n";
        cin>>flag;
        insurance = (flag == 'y');
        cout<<"Is the customer a priority boarder. y/n? \n";
        cin>>flag;
        priorityBoarding = (flag == 'y');
    }
    
    // 原有的get、edit、update方法保持不变
};

2. 修正OrderFlight类的方法错误

  • addPassenger方法中,传递参数错误,应直接传入order而非&order:
int addPassenger(Order *order)
{
    passenger = new Passenger(order);
    return 0;
}
  • removePassanger方法中,删除指针的方式错误,应直接删除指针本身而非指针的地址:
int removePassanger()
{
    delete passenger;
    passenger = nullptr; // 置空避免野指针
    return 0;
}

3. 修正OrderFlight类的继承逻辑

OrderFlight同时继承Order和Passenger会导致多重继承的冗余问题,同样应改为组合方式,比如保留Order继承,关联Passenger成员即可,避免不必要的继承。


内容的提问来源于stack exchange,提问作者Aratrik Chakraborti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 03:03:21