Class header + implementation

What am I doing wrong here? I keep getting compilation error when I try to run it in codelab (turings craft)

Instructions:

Write an implementation (.cpp file) of the GasTank class from the previous exercise. Full class specification: Data element named double. A constructor that has no parameters. The constructor initializes the number of data members to 0. A function named addGas that takes a double parameter. The value of the sum instance variable is incremented by the parameter value. A function named useGas that takes a double parameter. The value of the sum data member is decremented by the parameter value. A function named getGasLevel that takes no parameters. getGasLevel returns the value of the sum 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;}

      

+2


source to share


5 answers


You have specified both the class declaration and its functional implementations. How much of the code is in the header file and how much 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 cannot create an object of the class. Try changing the visibility of the corresponding functions to public

.

Make sure that the function signatures in the class declaration and class definition match. So if you declare

class GasTank {
    // ...
public:
    void addGas(double);
    // ...
};

      

make sure you define



void GasTank::addGas(double d)
{
    // ...
}

      

but not

double GasTank::addGas(double d)
{
    // ...
}

      

One last thing: format your code correctly. There is then a higher chance that you (and we) will be in a good mood while reading your code.

+1


source


The return values ​​of your ad 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;
}

      

+2


source


class

Default access mode private

. You need to make at least your constructor public and possibly member functions public

:

class GasTank
{
    double amount;
public:
    GasTank();
    // ...      
};

      

Inverse types for your definitions also don't match declarations.

+1


source


There are a few things wrong here:

  • You declare useGas and addGas to return "void", but in the definitions you say it returns "double".
  • If you intend to return "double", you do not "return" anything.
  • You need to use the "public:" keyword for your method access, otherwise it defaults to "private:"
  • addGas and useGas and the number of increments.
0


source


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;}

      

0


source







All Articles