0

I am trying to run a test in the main function, but the error "you cannot overload the main () function"is displayed.

#define CATCH_CONFIG_RUNNER // -- main() создавать нужно --
#include "catch.hpp"
int main(int argc, char* argv[])
{
    setlocale(LC_ALL, "Russian");
    int result = Catch::Session().run(argc, argv);
    system("pause");
    return result;
}
ANurbaev
  • 17
  • 3
  • Possible duplicate of [Can we overload main() function in C++?](https://stackoverflow.com/questions/5282151/can-we-overload-main-function-in-c) – sh.seo May 04 '19 at 15:42
  • @devdotlog But there's only one main function in my code. – ANurbaev May 04 '19 at 15:52

1 Answers1

0

You should use Catch in some other way. Something like that worked for me:

#include <iostream> // some standard includes, whatever you need

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEST_CASE("My first test") {
    // --- test code here ---
}

TEST_CASE("My second test") {
    // --- test code here ---
}

Try the framework's tutorial to learn more =)

MasterAler
  • 1,614
  • 3
  • 23
  • 35
  • After running this code, the console window quickly disappears. So I'm trying to run it through the main function. – ANurbaev May 04 '19 at 16:13
  • This looks like a problem with your IDE. What editor are you using to run the program? Find an option in your IDE like "make the console window persist after program termination". – KamilCuk May 04 '19 at 17:26
  • This answer and the tutorial don't explain why he's failing to use his own `main()`, like he should be able to do according to this part of the docs: https://github.com/catchorg/Catch2/blob/master/docs/own-main.md#top. – SiggiSv May 05 '19 at 22:10
  • Took `catch.hpp` + _exact_ code from the question. Worked just fine. I meant that if he's got problems with `main()` overloading, he could try a simplier approach first, as @KamilCuk correctly said, console window closing is more about IDE config than about framework usage. – MasterAler May 05 '19 at 23:06