5

I get this error in a C++ file where I am writing some tests:

error: no member named 'Session' in namespace 'Catch'
        testResult = Catch::Session().run(test_argc, test_argv);
                     ~~~~~~~^

Looking at the catch.hpp single header file, I noticed that the code that should implement the Session() member function is greyed out, probably because of an #ifdef somewhere, which I cannot find.

Is there any macro to set to use the Session class?

Catch versions: 1.5.3 and 1.5.6.

Reference: https://github.com/philsquared/Catch/blob/master/docs/own-main.md

jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
Pietro
  • 12,086
  • 26
  • 100
  • 193
  • 2
    obligatory sanity check: you *are* defining `CATCH_CONFIG_RUNNER` before you `#include` catch.hpp, right? – jaggedSpire Jun 29 '16 at 17:00
  • @jaggedSpire - Yes, just once, in the file where I define main(), which is not the one where I write the tests. – Pietro Jun 29 '16 at 17:14
  • and the compilation error is in the file where you define main(), correct? – jaggedSpire Jun 29 '16 at 17:18
  • No, it is in the file where I call Catch::Session(). – Pietro Jun 29 '16 at 17:28
  • It doesn't look like you're supposed to call `Catch::Session()` outside of the file where you're defining your own main, or even construct it. The documentation explicitly says there should only ever be once instance of it: "`Catch::Session session; // There must be exactly once instance`" Presumably, preventing it from being defined in non-main files is meant to prevent violation of that requirement. – jaggedSpire Jun 29 '16 at 17:33
  • You are right. I called it just once, but in a different file. Now it works. If you write it as an answer, I will accept it. Thank you! – Pietro Jun 29 '16 at 17:39
  • Done. Would you mind editing your question to include the information about where you were attempting to access `Catch::Session`? – jaggedSpire Jun 29 '16 at 17:46

2 Answers2

4

You're attempting to call the constructor of Catch::Session from a file where you're not defining your own main to execute. According to the documentation on defining your own main, there is supposed to be only one instance of Catch::Session:

Catch::Session session; // There must be exactly once instance

It's likely Catch is preventing construction of Catch::Session in translation units where it can't be used in a custom main definition (since that's where it's supposed to be used), to prevent exactly the mistake you've made from compiling.

jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
1

Reference to https://github.com/catchorg/Catch2/blob/master/docs/own-main.md

You can only provide main in the same file you defined CATCH_CONFIG_RUNNER.

#define CATCH_CONFIG_RUNNER
#include "catch.hpp"

int main( int argc, char* argv[] ) {
  // global setup...

  int result = Catch::Session().run( argc, argv );

  // global clean-up...

  return result;
}
Jagger Yu
  • 577
  • 4
  • 5