Reputation: 381
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;}
Upvotes: 2
Views: 8840
Reputation: 55
To be completely accurate this would be the correct code as per the instructions
class GasTank
{
public:
GasTank()
{amount=0;}
void addGas(double);
void useGas(double b)
{amount-=b;}
double getGasLevel()
{return amount;}
private:
double amount;
};
void GasTank::addGas(double a)
{amount+=a;}
Upvotes: 0
Reputation: 347226
The return values of your declaration must match the return values of your definition.
So you want:
class GasTank
{
public://<--- To use your GasTank you'll have to make the members public
GasTank();//<-- Especially the contructor
void addGas(double);
void useGas(double);
double getGasLevel();
private:
double amount;
};
GasTank::GasTank()
{//<--- read up on initializer lists so you can initialize amount here
amount=0;
}
void GasTank::addGas(double a) //<-- Return type fixed here
{
amount+=a;
}
void GasTank::useGas(double a) //<-- Return type fixed here
{
amount+=a;//<--- See something wrong here?
}
double GasTank::getGasLevel()
{
return amount;
}
Upvotes: 2
Reputation: 49156
There are several things wrong here:
Upvotes: 0
Reputation: 58677
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 class
the default visibility is private
. 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 to public
.
Make sure the function signatures in the class declaration and the class definition match. So, if you declare
class GasTank {
// ...
public:
void addGas(double);
// ...
};
make sure you define
void GasTank::addGas(double d)
{
// ...
}
and not
double GasTank::addGas(double d)
{
// ...
}
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.
Upvotes: 1
Reputation: 98984
The access mode for class
is by default private
. You need to make at least your constructor public and possibly the member functions public
:
class GasTank
{
double amount;
public:
GasTank();
// ...
};
The return types for your definitions also don't match the declarations.
Upvotes: 1