tags:

views:

124

answers:

4

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;}
+1  A: 

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.

Georg Fritzsche
+2  A: 

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;
}
Brian R. Bondy
+1  A: 

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.

wilhelmtell
i feel dumb. thanks.
codefail
A: 

There are several things wrong here:

  1. You declare useGas and addGas as returning 'void', but in the definitions you say it returns 'double'.
  2. If you intend to return 'double', you don't "return" anything.
  3. You need to use the "public:" keyword for your method access, or else it defaults to "private:"
  4. addGas and useGas both increment amount.
Stephen