为什么下面的代码给我一个关于抽象类的错误?

发布于 2024-10-10 07:21:50 字数 1901 浏览 5 评论 0原文

#include <iostream>
using namespace std;

class Vehicle
{
public:
    Vehicle() {};
    virtual ~Vehicle() {};
    virtual void Move() = 0;
    virtual void Haul() = 0;
};

class Car : public Vehicle
{
public:
    Car() {};
    virtual ~Car() {};
    virtual void Move(int m) { cDist = m;
    cout << "Vehicle moves " << cDist << " miles/hour\n"; }
    virtual void Haul() { cout << "Car's Hauling!\n"; }
private:
    int cDist;
};

class Bus : public Car
{
public:
    Bus() {};
    virtual ~Bus() {};
    void Move(int m) { Move(m); }
    void Haul() { "Bus is Hauling!\n"; }
};

int main()
{
    Vehicle* ptCar = new Car;
    Vehicle* ptBus = new Bus;
    ptCar->Move(1000);
    system("pause");
    return 0;
}

伙计们,为什么这段代码在尝试实例化 main 中的 Car 和 Bus 对象时出现错误?我无意创建汽车和公共汽车的抽象类,只是创建车辆的抽象类...... 任何帮助表示赞赏

1>------ Build started: Project: OperatorsTypes, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(36): error C2259: 'Car' : cannot instantiate abstract class
1>          due to following members:
1>          'void Vehicle::Move(void)' : is abstract
1>          c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(9) : see declaration of 'Vehicle::Move'
1>c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(37): error C2259: 'Bus' : cannot instantiate abstract class
1>          due to following members:
1>          'void Vehicle::Move(void)' : is abstract
1>          c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(9) : see declaration of 'Vehicle::Move'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <iostream>
using namespace std;

class Vehicle
{
public:
    Vehicle() {};
    virtual ~Vehicle() {};
    virtual void Move() = 0;
    virtual void Haul() = 0;
};

class Car : public Vehicle
{
public:
    Car() {};
    virtual ~Car() {};
    virtual void Move(int m) { cDist = m;
    cout << "Vehicle moves " << cDist << " miles/hour\n"; }
    virtual void Haul() { cout << "Car's Hauling!\n"; }
private:
    int cDist;
};

class Bus : public Car
{
public:
    Bus() {};
    virtual ~Bus() {};
    void Move(int m) { Move(m); }
    void Haul() { "Bus is Hauling!\n"; }
};

int main()
{
    Vehicle* ptCar = new Car;
    Vehicle* ptBus = new Bus;
    ptCar->Move(1000);
    system("pause");
    return 0;
}

guys, why this code is giving me errors when trying to instantiate an object of Car, and Bus in main. I didn't intended to create an abstract class of Car and Bus, only of Vehicle...
any help appreciated

1>------ Build started: Project: OperatorsTypes, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(36): error C2259: 'Car' : cannot instantiate abstract class
1>          due to following members:
1>          'void Vehicle::Move(void)' : is abstract
1>          c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(9) : see declaration of 'Vehicle::Move'
1>c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(37): error C2259: 'Bus' : cannot instantiate abstract class
1>          due to following members:
1>          'void Vehicle::Move(void)' : is abstract
1>          c:\users\jorge\documents\visual studio 2010\projects\shapes\operatorstypes\operatorstypes\main.cpp(9) : see declaration of 'Vehicle::Move'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅浅淡淡 2024-10-17 07:21:50

派生类中的 Move 函数具有不同的签名(它们都采用 int)。

要修复此问题,请在基类中添加一个 int 参数。

virtual void Move(int m) = 0

The Move function in your derived classes have a different signature (they both take an int).

To fix it, add an int parameter in the base class as well.

virtual void Move(int m) = 0
淡淡绿茶香 2024-10-17 07:21:50

Car 是抽象的,因为它不会重写纯虚拟 void Vehicle::Move()

void Car::Move(int) 不会覆盖 void Vehicle::Move(),因为它的参数列表不同。

Car is abstract because it does not override the pure virtual void Vehicle::Move().

void Car::Move(int) does not override void Vehicle::Move() because its parameter list is different.

梦魇绽荼蘼 2024-10-17 07:21:50

您缺少超类中的参数 m 。在带有函数重载的Vehicle中

virtual void Move(int) = 0;  // add the int

,aMethod()和aMethod(aArgument)是不同的。

You are missing the parameter m in super class. In Vehicle

virtual void Move(int) = 0;  // add the int

In C++ with function overloading, aMethod() and aMethod(aArgument) are not same.

余生一个溪 2024-10-17 07:21:50

Move 在抽象 Vehicle 类中采用 0 个参数,在 BusCarint 参数code> 所以它们是不同的函数。将 Vehicle 中的原型更改为

virtual void Move(int) = 0;

Move takes 0 arguments in the abstract Vehicle class and an int argument in Bus and Car so they are different functions. Change the prototype in Vehicle to

virtual void Move(int) = 0;
攒眉千度 2024-10-17 07:21:50

正如错误所示, CarBus 是抽象的,因为您没有重写其中的 void Vehicle::Move()Move(int) 是一个不同的函数。

As the error indicates, Car and Bus are abstract because you did not override void Vehicle::Move() in them. Move(int) is a different function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文