Classes & Constructors Question: What am I missing?



Write a full class definition for a class named GasTank , and containing the following members:
A data member named amount of type double .
A constructor that no parameters . The constructor initializes the data member amount to 0.
A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter .
A function named useGas that accepts a parameter of type double . The value of the amount data member is decreased by the value of the parameter .
A function named getGasLevel that accepts no parameters . getGasLevel returns the value of the amount data member.


I keep getting errors telling me my prototypes don't match any in the class. Not sure what I'm missing. Any help or advice would be appreciated.

Here is my code:


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
class GasTank
{
   private: 
	double amount;
	
   public: 
	GasTank();
	
    void addGas(double);
	void useGas(double);
	double getGasLevel();
	
};

double GasTank::addGas(double g)  
{
	amount = amount + g;
}

double GasTank::useGas(double g)  
{
	amount = amount - g;
}

GasTank::getGasLevel()  
{
	return amount;
}
It is actually being very specific; your actual definitions don't match the prototypes in the class.

On line 9, you declare addGas() as returning void, but on line 15 you have it return a double.

On line 10, you declare useGas() as returning void, but on line 20 you have it return a double.

On line 11 you declare getGasLevel() as returning a double, but on line 25 you haven't even specified a return value.
Very helpful thank you!

I am still get an error:

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
class GasTank
{
   private: 
	double amount;
	
   public: 
	GasTank();
	
    void addGas(double);
	void useGas(double);
	double getGasLevel();
	
};

void GasTank::addGas(double g)  
{
	amount += g;
}

void GasTank::useGas(double g)  
{
	amount -= g;
}

double GasTank::getGasLevel()  
{
	return amount;
}
Your not missing anything in the piece of code. If you still get an error, its not here
It would be useful also if you would tell us what the error is. ;)
You declare a default constructor on line 7, but you do not appear to have an implementation for it. Your implementation is required to set amount to zero per the instructions:
A constructor that no parameters . The constructor initializes the data member amount to 0.

Topic archived. No new replies allowed.