为什么下面的代码给我一个关于抽象类的错误?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
派生类中的
Move
函数具有不同的签名(它们都采用int
)。要修复此问题,请在基类中添加一个
int
参数。The
Move
function in your derived classes have a different signature (they both take anint
).To fix it, add an
int
parameter in the base class as well.Car
是抽象的,因为它不会重写纯虚拟void Vehicle::Move()
。void Car::Move(int)
不会覆盖void Vehicle::Move()
,因为它的参数列表不同。Car
is abstract because it does not override the pure virtualvoid Vehicle::Move()
.void Car::Move(int)
does not overridevoid Vehicle::Move()
because its parameter list is different.您缺少超类中的参数 m 。在带有函数重载的Vehicle中
,aMethod()和aMethod(aArgument)是不同的。
You are missing the parameter m in super class. In Vehicle
In C++ with function overloading, aMethod() and aMethod(aArgument) are not same.
Move
在抽象Vehicle
类中采用 0 个参数,在Bus
和Carint
参数code> 所以它们是不同的函数。将Vehicle
中的原型更改为Move
takes 0 arguments in the abstractVehicle
class and anint
argument inBus
andCar
so they are different functions. Change the prototype inVehicle
to正如错误所示,
Car
和Bus
是抽象的,因为您没有重写其中的void Vehicle::Move()
。Move(int)
是一个不同的函数。As the error indicates,
Car
andBus
are abstract because you did not overridevoid Vehicle::Move()
in them.Move(int)
is a different function.