Car Instrument Simulator

I keep getting errors on the following programming challenge, any help would be greatly appreciated.

Instructions of the assignment:

Design a set of classes that work together to simulate a car's fuel gauge and odometer. The classes you will design are:

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are:
--To know the car's current amount of fuel, in gallons.

--To report the car's current amount of fuel, in gallons.

--To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. (The car can hold a maximum of 15 gallons.)

--To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs.

The Odometer Class: This class will simulate the car's odometer. Its responsibilities are:
--To know the car's current mileage.

--To report the car's current mileage.

--To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.

--To be able to work with a FuelGauge object. It should decrease the FuelGauge object s current amount of fuel by 1 gallon for every 24 miles traveled. (The car's fuel economy is 24 miles per gallon.)

Demonstrate the classes by creating instances of each. Simulate lling the car up with fuel, and then run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print the car's current mileage and amount of fuel.

ERRORS: error LNK2019: unresolved external symbol "public: __thiscall FuelGauge::FuelGauge(void)" (??0FuelGauge@@QAE@XZ) referenced in function _main


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Main.cpp
#include <iostream>
#include "FuelGauge.h"
#include "Odometer.h"
using namespace std;

int main()
{
	FuelGauge fuel;
	Odometer odm(0, &fuel);

	for (int i = 0; i < 15; i++)
		fuel.incrementFuelTank();

	while (fuel.getCurrentAmountOfFuel() > 0)
	{
		odm.incrementcurrentMileage();
		cout << "Mileage: " << odm.getCurrentMileage() << endl;
		cout << "Fuel level" << fuel.getCurrentAmountOfFuel() << " gallons" << endl;
	}

	return 0;
}

// FuelGauge.h
using namespace std;

#ifndef FUELGAUGE_H
#define FUELGAUGE_H

class FuelGauge
{
private:
	int currentAmountOfFuel;
public:
	FuelGauge(int gallons)
	{
		currentAmountOfFuel = gallons;
	}
	FuelGauge();
	int getCurrentAmountOfFuel() const
	{
		return currentAmountOfFuel;
	}
	void incrementFuelTank()
	{
		if (currentAmountOfFuel< 15)
			currentAmountOfFuel++;
	}
	void decrementFuelTank()
	{
		if (currentAmountOfFuel > 0)
			currentAmountOfFuel--;
	}
};

#endif

// Odometer.h
#include "FuelGauge.h"
using namespace std;

#ifndef ODOMETER_H
#define ODOMTER_H

class Odometer
{
private:
	int currentMileage;
	FuelGauge *fuelG;
public:
	Odometer(int miles, FuelGauge *f)
	{
		currentMileage = miles;
		fuelG = f;
	}

	int getCurrentMileage()
	{
		return currentMileage;
	}
	void incrementcurrentMileage()
	{
		if (currentMileage < 999999)
			currentMileage++;
		if (currentMileage == 999999)
			currentMileage = 0;
	}
	void decrementcurrentMileage()
	{
		if (currentMileage > 24)
			currentMileage--;
	}
};

#endif 
Line 40: you hve declared constructor without parameters, but did not defined it.
And you are trying to call it on line 9;
I'm a little confused on how to fix this, would I define it inside the default constructor?
Last edited on
Just change it to this FuelGauge(){} Now you've defined the default constructor, but it doesnt do anything. Should fix what @MiiniPaa said.
It just outputs "Press any key to continue.."
Last edited on
It never enters the while loop

1
2
3
4
5
6
while (fuel.getCurrentAmountOfFuel() > 0)
	{
		odm.incrementcurrentMileage();
		cout << "Mileage: " << odm.getCurrentMileage() << endl;
		cout << "Fuel level" << fuel.getCurrentAmountOfFuel() << " gallons" << endl;
	}


Debug your code to try to figure out why
I went through my code and I can't seem to understand why. In my main function, doesn't my for loop increase my currentAmountOfFuel to 14?
Well no. You never call this constructor.

1
2
3
4
FuelGauge(int gallons)
{
	currentAmountOfFuel = gallons;
}


So you never fill the fuel.

Edit:

Change this

FuelGauge fuel;

to this

FuelGauge fuel(x); // replace x with the amount of gallons you want.
Last edited on
THe real reason why your code does not work (TarikNeaj fix will help, but it is important that you understand why is this happening):

1
2
3
4
FuelGauge fuel;
//What will fuel.getCurrentAmountOfFuel() return?
//Why? Where in your code you guarantee it?
//What if it returns -19365? 
wouldn't fuel.getCurrentAmountOfFuel not return anything, since the code is not assigning anything to it
It cannot not return anything, as it requred for it to return int. As code is not assigning anything to it, we cannot say what is in it. It can be anything.
You have two ways to circumvent it:
1) Remove default constructor altogether, forcing user to explicitely state how much fuel is in newly created tank and use TarikNeaj code to create vriable.
2) Initialize currentAmountOfFuel to some value in default constructor.
I understand that, but I'm a little confused on how to work between classes and the main function
---------------------------------------------------------------------------------------------
I messed around with my program, and got it to work. Thanks for the help! :D
Last edited on
Topic archived. No new replies allowed.