what am I doing wrong here? I keep on getting a compilation error when I try to run this in codelab (turings craft)
Instructions:
Write the implementation (.cpp file) of the GasTank class of the previous exercise. The full specification of the class is:
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.
class GasTank{
double amount;
GasTank();
void addGas(double);
void useGas(double);
double getGasLevel();};
GasTank::GasTank(){
amount=0;}
double GasTank::addGas(double a){
amount+=a;}
double GasTank::useGas(double a){
amount+=a;}
double GasTank::getGasLevel(){
return amount;}
You listed both the class declaration and its function implementations. What part of code is in the header file, and what part of it is in the implementation file?
Also, recall that for a
classthe default visibility isprivate. Therefore, all your functions are private — including the constructor! — so you can’t instantiate an object of the class. Try changing the visibility of the relevant functions topublic.Make sure the function signatures in the class declaration and the class definition match. So, if you declare
make sure you define
and not
One last thing: do indent your code properly. There’s then a higher chance you marker (and us) will be in a good mood while reading your code.