RaceCar.obj : error LNK2019: unresolved external symbol "public: void __thiscall Car::setSpeed(int)" (?setSpeed@Car@@QAEXH@Z)

StackOverflow https://stackoverflow.com/questions/23372920

  •  12-07-2023
  •  | 
  •  

Question

im lost because i am copying straight out a book and i have went over it multiple times ad cant find a difference in my code and theirs. i need to learn what causes this problem so i can solve it in the future. the code keeps telling me there is something wrong with RaceCar setSpeed somewhere
#include "stdafx.h" #include using 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;
}
Was it helpful?

Solution

Try making the the setSpeed(int) function in the Car class a virtual function. This will allow you to override the function in the RaceCar class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top