Help with Space Age exercise

Heya, trying to solve the Space Age exercise. I’ve come up with a few solutions that work and compile locally using gcc and clang but I can’t get them to compile within Exercism.

I’m wondering if this is because I need to be using specific syntax, and if so, does anyone have a hint or two they could share to get me on the right track?

Do they pass the tests locally?

Can you share your code and the error? Please use code blocks. Please do not use screenshots.

Heya, yes it does compile locally and return the correct answers. I’m using Ubuntu and have compiled the code using both gcc and clang. This is one example of the code:

#include<iostream>
#include<string>
#include<cmath>

namespace SpaceAge {

  class space_age {
  public:
    double seconds{};
    space_age(double seconds);
    double on_earth();
    double on_mercury();
    double on_venus();
    double on_mars();
    double on_jupiter();
    double on_saturn();
    double on_uranus();
    double on_neptune();

  private:
    double earth_seconds{31557600};
  };

  double round_year(double val, int decimal_places) {
    double power_of_10 = pow(10.0, static_cast<double>(decimal_places));
    return floor(val * power_of_10 + 0.5) / power_of_10;
  }

  space_age::space_age(double seconds) { this->seconds = seconds; }
  double space_age::on_earth() { return round_year(seconds / earth_seconds, 2); }
  double space_age::on_mercury() { return round_year((seconds / earth_seconds) / 0.2408467, 2); }
  double space_age::on_venus() { return (seconds / earth_seconds) / 0.61519726; }
  double space_age::on_mars() { return (seconds / earth_seconds) / 1.8808158; }
  double space_age::on_jupiter() { return (seconds / earth_seconds) / 11.862615; }
  double space_age::on_saturn() { return (seconds / earth_seconds) / 29.447498; }
  double space_age::on_uranus() { return (seconds / earth_seconds) / 84.016846; }
  double space_age::on_neptune() { return (seconds / earth_seconds) / 164.79132; }
}

int main() {
  SpaceAge::space_age age(1000000000);
  std::cout << age.on_earth() << std::endl;
  std::cout << age.on_mercury() << std::endl;
  std::cout << age.on_venus() << std::endl;
  std::cout << age.on_mars() << std::endl;
  return 0;
} 

This is the code I’ve been testing locally. On Exercism it is split between a header file and .cpp file as requested by the exercise. The Exercism compiler error readout is quite long so I’ll not include all of it here. But the elements I have not included are the repeats of the other errors in relation to other planets.

We received the following error when we ran your code:

In file included from /usr/local/include/catch2/benchmark/catch_benchmark.hpp:14,
                 from /usr/local/include/catch2/benchmark/catch_benchmark_all.hpp:24,
                 from /usr/local/include/catch2/catch_all.hpp:25,
                 from /tmp/space-age/space_age_test.cpp:3:
/tmp/space-age/space_age_test.cpp: In function 'void CATCH2_INTERNAL_TEST_0()':
/tmp/space-age/space_age_test.cpp:11:24: error: expression cannot be used as a function
   11 |     REQUIRE(age.seconds() == 1000000);
      |             ~~~~~~~~~~~^~
In file included from /usr/local/include/catch2/catch_test_macros.hpp:11,
                 from /usr/local/include/catch2/catch_template_test_macros.hpp:20,
                 from /usr/local/include/catch2/catch_all.hpp:36:
/tmp/space-age/space_age_test.cpp:11:24: error: expression cannot be used as a function
   11 |     REQUIRE(age.seconds() == 1000000);
      |             ~~~~~~~~~~~^~
/tmp/space-age/space_age_test.cpp:11:24: error: expression cannot be used as a function
   11 |     REQUIRE(age.seconds() == 1000000);
      |             ~~~~~~~~~~~^~
In file included from /usr/local/include/catch2/matchers/catch_matchers.hpp:11,
                 from /usr/local/include/catch2/matchers/catch_matchers_all.hpp:23,
                 from /usr/local/include/catch2/catch_all.hpp:126:
/tmp/space-age/space_age_test.cpp: In function 'void CATCH2_INTERNAL_TEST_2()':
/tmp/space-age/space_age_test.cpp:22:30: error: passing 'const space_age::space_age' as 'this' argument discards qualifiers [-fpermissive]
   22 |     REQUIRE_THAT(age.on_earth(), Catch::Matchers::WithinAbs(31.69, accuracy));
      |                  ~~~~~~~~~~~~^~
In file included from /tmp/space-age/space_age_test.cpp:1:
/tmp/space-age/space_age.h:12:12: note:   in call to 'double space_age::space_age::on_earth()'
   12 |     double on_earth();
      |            ^~~~~~~~
/tmp/space-age/space_age_test.cpp: In function 'void CATCH2_INTERNAL_TEST_4()':
/tmp/space-age/space_age_test.cpp:28:30: error: passing 'const space_age::space_age' as 'this' argument discards qualifiers [-fpermissive]
   28 |     REQUIRE_THAT(age.on_earth(), Catch::Matchers::WithinAbs(67.65, accuracy));
      |                  ~~~~~~~~~~~~^~
/tmp/space-age/space_age.h:12:12: note:   in call to 'double space_age::space_age::on_earth()'
   12 |     double on_earth();
      |            ^~~~~~~~
/tmp/space-age/space_age_test.cpp:29:32: error: passing 'const space_age::space_age' as 'this' argument discards qualifiers [-fpermissive]
   29 |     REQUIRE_THAT(age.on_mercury(),
      |                  ~~~~~~~~~~~~~~^~
/tmp/space-age/space_age.h:13:12: note:   in call to 'double space_age::space_age::on_mercury()'
   13 |     double on_mercury();
      |            ^~~~~~~~~~
/tmp/space-age/space_age_test.cpp: In function 'void CATCH2_INTERNAL_TEST_6()':
/tmp/space-age/space_age_test.cpp:36:30: error: passing 'const space_age::space_age' as 'this' argument discards qualifiers [-fpermissive]

Are you running the unit tests locally?

Looking at the first error, it is calling seconds() but you’re code doesn’t have a second function. Running the code locally without the unit tests isn’t very helpful. It shows the code compiles, but that doesn’t mean it does what the tests expect it to do.

Yeah, that’s what I guessed but there was so little to go on in the exercise outline I wasn’t sure what I might be doing wrong. And yes, I am running the code locally without unit tests just so I can play around with things. But noted that it is not very helpful.

I’m pretty new to programming so still learning how to read all of the errors (and not sure if I understood it all right). I’ll keep plugging away.

Instructions to get the Exercism code locally: https://exercism.org/docs/using/solving-exercises/working-locally

If you’re working locally, you really ought to run the tests. When in doubt about the exercise, you should run the tests or read the tests.

1 Like

In case you haven’t already solved your problem, please read the quoted part of the error message.
The compiler tells you that the “expression cannot be used as a function”, prints the line where the error occurred, and visually points to age.seconds.

In this statement (line 11 in space_age_test.cpp) the test tries to call age.second.
But in your solution seconds is a member variable of type double, not a function that can be called.

Does that help with the problem?