RaceCar.obj:错误LNK2019:未解析的外部符号" public:void __thiscall Car :: setSpeed(int)" (?setSpeed @汽车@@ QAEXH @ Z)

时间:2014-04-29 18:49:42

标签: visual-studio-2010 visual-c++

我迷失了,因为我正在直接复制一本书而且我已多次检查它,因为我的代码和他们的代码无法找到差异。我需要了解导致这个问题的原因,以便我将来能够解决这个问题。代码一直告诉我RaceCar有问题 setSpeed在某处     #include“stdafx.h”     #包括     使用namespace std;

class Car
{
protected:
bool isIgnitionOn;
int speed;
public:
void turnIgnitionOn();
void turnIgnitionOff();
void setSpeed(int);
void showCar();
};
void Car::showCar()
{
if(isIgnitionOn)
    cout << " ignition is on" ;
else 
    cout << " ignition is off" ;
cout << "Speed is " << speed << endl;
}
void Car::turnIgnitionOn()
{
isIgnitionOn = true;
}
void Car::turnIgnitionOff()
{
speed = 0;
isIgnitionOn = false;
}

class RaceCar: public Car
{
public:
void setSpeed(int mph);
};
void RaceCar::setSpeed(int mph)
{
const int MAX_SPEED = 200;
if(isIgnitionOn)
    if(mph<= MAX_SPEED)
        speed = mph;
    else
        speed = MAX_SPEED;
else
    cout << " cant set speed - ignition is off!" << endl;
}

int main()
{
Car myCar;
RaceCar aRaceCar;
myCar.turnIgnitionOn();
myCar.setSpeed(80);
cout << " car at 80 mph: " ;
myCar.showCar();
aRaceCar.turnIgnitionOn();
aRaceCar.setSpeed(80);
cout << " race car at 80 mph: ";
aRaceCar.showCar();


return 0;
}

1 个答案:

答案 0 :(得分:0)

尝试将Car类中的setSpeed(int)函数设为虚拟函数。这将允许您覆盖RaceCar类中的函数。