灵泽7331 2021-03-07 17:14 采纳率: 25%
浏览 48

c++虚基类有关的参数传递出现了问题,出问题处已经备注,大神们可怜可怜孩子

#include <iostream>
using namespace std;
/*
定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。
自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类。
请将vehicle设置为虚基类进行实验
输出:::::
vehicle is running...
vehicle is stopped...
vehicle is running...
vehicle is stopped...
vehicle is running...
vehicle is stopped...
vehicle is running...
vehicle is stopped...
*/
class vehicle///虚基类vehicle
{
    public:
    vehicle(float maxspeed,float weight):MaxSpeed(maxspeed),Weight(weight){}
    vehicle(){}
    void Run();
    void Stop();
    private:
    float MaxSpeed,Weight;
};
void vehicle::Run()
{
    cout<<"vehicle is running..."<<endl;
}
void vehicle::Stop()
{
    cout<<"vehicle is stopped..."<<endl;
}
class bicycle:virtual public vehicle///////bicycle继承vehicle
{
    float Height;
public:
    bicycle(float height,float maxspeed,float weight):vehicle(maxspeed,weight),Height(height){}
    bicycle(){}
};
class motorcar:virtual public vehicle////motorcar继承vehicle
{
    int SeatNum;
public:
    motorcar(int seatnum,float maxspeed,float weight):vehicle(maxspeed,weight),SeatNum(seatnum){}
    motorcar(){}
};
class motorcycle:public bicycle, public motorcar//////motorcycle继承bicycle和motorcycle
{
    public:

        motorcycle(float height, int seatnum, float maxspeed, float weight): bicycle(height), motorcar(seatnum),vehicle(maxspeed,weight){}
        ///此处报错此处报错此处报错此处报错此处报错此处报错此处此处报错此处报错此处报错
    //////////////////报错此处报错此处报错bicycle(height), motorcar(seatnum),此处报错,不知道为什么
        motorcycle(){}

};

void fun(vehicle *p)
{
    p->Run();
    p->Stop();
}
int main()
{
    vehicle v;
    bicycle by;
    motorcar mc;
    motorcycle mcy;
    fun(&v);
    fun(&by);
    fun(&mc);
    fun(&mcy);
    system("pause"); 
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 幻灰龙 2021-03-07 17:43
    关注

    类 `motorcar` 提供了两个构造函数

    • motorcar(int seatnum,float maxspeed,float weight):vehicle(maxspeed,weight),SeatNum(seatnum){}
    • motorcar(){}

    但是你继承的时候,提供给 motorcar 的参数个数只有1个?上述两个构造函数,一个接受 3 个参数,一个没有参数,不匹配就报错了,其他同理:

    motorcycle(float height, int seatnum, float maxspeed, float weight)
    
    : bicycle(height), motorcar(seatnum), vehicle(maxspeed, weight) {}
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备