Unresolved External Symbol

I'm trying to write a code for the following prompt; Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.

The problem I'm having is its only showing negative speeds and its cycling through 25 times, it should only display the speed 10 times. Someone please help this coding novice!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
#include<string>
using namespace std;

class Car
{
private:
	double YearModel;
	string Make;
	double Speed;

public:
	Car(double, string, double);
	double getSpeed();
	double getModel();
	void accelerate();
	void brake();
};

double Car::getSpeed()
{
	return Speed;
}

Car::Car(double YearModel, string Make, double Speed = 0)
{

}

void Car::accelerate()
{
	Speed += 5;

	if (Speed > 5)
		Speed -= 5;
	else Speed = 0;
}

void Car::brake()
{
	Speed -= 5;
}

int main()
{
	double YearModel;
	string Make;
	cout << "Enter the vehicles year and make ";
	cin >> YearModel >> Make;

	Car myCar(YearModel, Make);
	for (int a = 0; a < 5; a++)
	{
		myCar.accelerate();
		cout << "The current speed of the vehicle is: " << myCar.getSpeed() << endl;

		for (int b = 0; b < 5; b++)
		{
			myCar.brake();
			cout << "The current speed of the vehicle is: " << myCar.getSpeed() << endl;

		}
	}
	system("Pause");
	return 0;
}
Last edited on
According the the cpp.sh shell compiler, you're calling brake() but you never defined it.
closed account (48T7M4Gy)
you don't have a brake() method in your car class
^^ What they said. You're calling brake on line 54, but you never defined it. If you look on line 49 you called accelerate, and you defined it on line 30.

Edit: lol my line numbers seem to be a bit off.. fail
Last edited on
oops, I was in the midst of editing it when all of you posted. I already realized my first issue and fixed it. I defined the break function.
I ended up editing the entire post before I saw that all of you had responded. Now my current issue is its displaying the speed 25 times instead of 10 times AND all the speeds are negative. The times should display as such 5, 10, 15, 20, 25, 20, 15, 10, 5, 0
My tip for when you're dealing with classes, @OP. Always implement your class methods. It doesn't matter if they're just barebones, like if you had a function that returned an int:
1
2
3
4
int foo( int a )
{
    return 0;
}

Just do that. It won't do what you want, but you won't get linker errors this way. There's other things that'll get you linker errors like including header files repeatedly (although there is a way), etc.
Topic archived. No new replies allowed.