#Problem with the .h file in an C++ exercise

10 messages · Page 1 of 1 (latest)

sage canopy
#

I have a problem with the Space Age exercise because I don't know what to put in the .h file. I know that when I only need to program functions I just declare them there. For example for the reverse string code

namespace reverse_string {
        std::string reverse_string(std::string text){
        int size = text.size();
        std::string newString;
    
        for(int i = 0; i < size; i ++){
            newString += text[size - i - 1];
        }
    
        return newString;
    }
}  // namespace reverse_string
```cpp
You needed to put this into the .h file
```cpp
#if !defined(REVERSE_STRING_H)
#define REVERSE_STRING_H
#include <string>

namespace reverse_string {
    std::string reverse_string(std::string text);
}  // namespace reverse_string

#endif // REVERSE_STRING_H
```cpp
But in the Space Age exercise you need to create a function and I have tried to put everything in the .h file and nothing works. This is my, I think, correct code
```cpp
#include "space_age.h"

namespace space_age {
    class space_age{
        public:
        int seconds(){
            return sec;
        }

        space_age(int newSec){
            sec = newSec;
        }

        double on_earth(){
            return sec / 31557600.00;
        }

        double on_mercury(){
            return on_earth() / 0.2408467;
        }

        double on_venus(){
            return on_earth() / 0.61519726;
        }

        private:
        int sec;
    };

}  // namespace space_age
```cpp
And this is my .h file that does not work
```cpp
#if !defined(SPACE_AGE_H)
#define SPACE_AGE_H

namespace space_age {
    class space_age {
    public:
        space_age(int newSec);
        int seconds();
        double onEarth();
        double onMercury();
        double onVenus();
    private:
        int sec;
    };
}  // namespace space_age

#endif // SPACE_AGE_H
```cpp

I am all out of ideas what to do. Can you please tell me what should I put there?
#

I had to erase the other noPlanet times so the message would not be too long. In the actual code I have all of them implemented

kindred copper
#

There are two main ways to organize the code for this specific exercise. I recommend defining the class space_age and declaring its member functions in the .h file and defining the member functions in the .cpp file.

#

Your .h file is a good start but there are four issues:

  • An int is not large enough to represent the ages (in seconds) that are used in the tests. You need a larger type.
  • The member functions for the age on the planets are named onEarth(), etc., in "lowerCamelCase". But the tests require them in "snake_case": on_earth() etc.
  • Currently the .h file only defines four member functions, seconds() and three others for three planets in our solar system. The tests require member functions for all planets in our solar system.
  • If you take a look at the tests you'll see that they define a const variable of type space_age::space_age, and then call its member functions. That means only const member functions can be called, you will have to make the member functions seconds(), on_earth(), etc. const.
#

If you follow my advice you will have to define the member functions in the .cpp file.
Do not define the class space_age a second time, instead define each member function like this:

namespace space_age {
    int space_age::seconds() {
        return sec;
    }
}
sage canopy
#

I followed the intructions and I am still making a mistake somewhere. Right now my .cpp file looks like this

#
#include "space_age.h"

namespace space_age {
        const long int space_age::seconds(){
            return sec;
        }

        space_age::space_age(long int newSec){
            sec = newSec;
        }

        const double space_age::on_earth(){
            return sec / 31557600.00;
        }

        const double space_age::on_mercury(){
            return on_earth() / 0.2408467;
        }

        const double space_age::on_venus(){
            return on_earth() / 0.61519726;
        }

        const double space_age::on_mars(){
            return on_earth() /  1.8808158;
        }

        const double space_age::on_jupiter(){
            return on_earth() / 11.862615;
        }

        const double space_age::on_saturn(){
            return on_earth() / 29.447498;
        }

        const double space_age::on_uranus(){
            return on_earth() / 84.016846;
        }

        const double space_age::on_neptune(){
            return on_earth() / 164.79132;
        }
    };

}  // namespace space_age
```cpp
and my .h file like this
```cpp
#if !defined(SPACE_AGE_H)
#define SPACE_AGE_H

namespace space_age {
    class space_age {
    public:
        space_age(long int newSec);
        const long int seconds();
        const double on_earth();
        const double on_mercury();
        const double on_venus();
        const double on_mars();
        const double on_jupiter();
        const double on_saturn();
        const double on_uranus();
        const double on_neptune();

    private:
        long int sec;
    };
}  // namespace space_age

#endif // SPACE_AGE_H
```cpp
kindred copper
#

You've added the const to the return type but actually you have to make the member function itself const.
That allows it to be called even if the instance is const, it's basically a promise that the member function won't modify the state of the instance.
For more about const member functions you might want to visit LearnCpp.com.

#

Also, the test age_in_neptune_year constructs a space_age instance with the value 8210123456.
The smallest integral data type in the standard that is guaranteed to be able to hold that 33-bit value is long long int (see cppreference).
Or you could use std::int64_t from <cstdint>.

sage canopy
#

Thank you very much, I finally passed it