English 中文(简体)
下面的法典为什么给我一个抽象的类别留下错误?
原标题:Why is the following code giving me an error about an abstract class?
#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
"; }
    virtual void Haul() { cout << "Car s Hauling!
"; }
private:
    int cDist;
};

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

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:usersjorgedocumentsvisual studio 2010projectsshapesoperatorstypesoperatorstypesmain.cpp(36): error C2259:  Car  : cannot instantiate abstract class
1>          due to following members:
1>           void Vehicle::Move(void)  : is abstract
1>          c:usersjorgedocumentsvisual studio 2010projectsshapesoperatorstypesoperatorstypesmain.cpp(9) : see declaration of  Vehicle::Move 
1>c:usersjorgedocumentsvisual studio 2010projectsshapesoperatorstypesoperatorstypesmain.cpp(37): error C2259:  Bus  : cannot instantiate abstract class
1>          due to following members:
1>           void Vehicle::Move(void)  : is abstract
1>          c:usersjorgedocumentsvisual studio 2010projectsshapesoperatorstypesoperatorstypesmain.cpp(9) : see declaration of  Vehicle::Move 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳回答

<代码>Move在你的衍生类别中具有不同的签字(两者均采用int)。

除此以外,在基数类别中添加“<条码>int参数”。

virtual void Move(int m) = 0
问题回答

<代码>Car是抽象的,因为它并不凌驾于单纯的<密码>上:Move(。

<代码>ave Car:Move(int) is not overe ave car:Move(<>/code>,因为其参数清单不同。

你们没有超级的参数。 车辆

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

在C++,超负荷运行,Method()和Method(aArgument)并不相同。

<代码>Move在抽象的<代码>Vehicle类别和int在<代码>Bus和上提出0论点,因此它们是不同的功能。 更改<代码>Vehicle的原型

virtual void Move(int) = 0;

如该错误所示,CarBus是抽象的,因为您在其中没有超越<条码>。 <代码>Move(int)是一个不同的功能。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签