Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error "error: expected ';' at end of declaration list" using Catch testing framework in C++

I'm trying to learn C++ by implementing some simple algorithms in it. In order to test these algorithms, I'd like to use Catch2. Here is a program I came up for binary search:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <iostream>
using namespace std;

int binary_search_recursive(int A[], int key, int low, int high) {
    if (low > high) {
        return -1;
    }
    int mid = (low + high)/2;
    if (A[mid] == key) {
        return mid;
    } else if (key < A[mid]) {
        return binary_search_recursive(A, key, low, mid-1);
    } else {
        return binary_search_recursive(A, key, mid+1, high);
    }
}

int binary_search(int A[], int key, int len) {
  return binary_search_recursive(A, key, 0, len - 1);
}


// int main() {
//  int A[] = {1, 2, 4};
//  int key = 4;
//  int len = 3;
//  cout << binary_search(A, key, len);
//  return 0;
// }

TEST_CASE("Binary search works", "[binary_search]") {
    REQUIRE(1 == 1);
}

where I've copied the catch.hpp single header file into the same directory. The problem is that when I try to compile it using the g++ command on my Mac, I get the following error:

$ g++ BinarySearch.cpp
In file included from BinarySearch.cpp:2:
./catch.hpp:380:63: error: expected ';' at end of declaration list
        SourceLineInfo( char const* _file, std::size_t _line ) noexcept
                                                              ^
./catch.hpp:390:27: error: expected ';' at end of declaration list
        bool empty() const noexcept;
                          ^
./catch.hpp:391:63: error: expected ';' at end of declaration list
        bool operator == ( SourceLineInfo const& other ) const noexcept;
                                                              ^
./catch.hpp:392:62: error: expected ';' at end of declaration list
        bool operator < ( SourceLineInfo const& other ) const noexcept;
                                                             ^
./catch.hpp:496:16: error: unknown type name 'constexpr'
        static constexpr char const* const s_empty = "";
               ^
./catch.hpp:496:26: error: expected member name or ';' after declaration
      specifiers
        static constexpr char const* const s_empty = "";
        ~~~~~~~~~~~~~~~~ ^
./catch.hpp:499:20: error: expected ';' at end of declaration list
        StringRef() noexcept
                   ^
./catch.hpp:518:58: error: expected ';' at end of declaration list
        StringRef( char const* rawChars, size_type size ) noexcept
                                                         ^
./catch.hpp:542:38: error: expected ';' at end of declaration list
        void swap( StringRef& other ) noexcept;
                                     ^
./catch.hpp:545:9: error: 'auto' not allowed in function return type
        auto operator == ( StringRef const& other ) const noexcept -> bool;
        ^~~~
./catch.hpp:545:58: error: expected ';' at end of declaration list
        auto operator == ( StringRef const& other ) const noexcept -> bool;
                                                         ^
./catch.hpp:546:9: error: 'auto' not allowed in function return type
        auto operator != ( StringRef const& other ) const noexcept -> bool;
        ^~~~
./catch.hpp:546:58: error: expected ';' at end of declaration list
        auto operator != ( StringRef const& other ) const noexcept -> bool;
                                                         ^
./catch.hpp:548:9: error: 'auto' not allowed in function return type
        auto operator[] ( size_type index ) const noexcept -> char;
        ^~~~
./catch.hpp:548:50: error: expected ';' at end of declaration list
        auto operator[] ( size_type index ) const noexcept -> char;
                                                 ^
./catch.hpp:551:9: error: 'auto' not allowed in function return type
        auto empty() const noexcept -> bool {
        ^~~~
./catch.hpp:551:27: error: expected ';' at end of declaration list
        auto empty() const noexcept -> bool {
                          ^
./catch.hpp:559:9: error: 'auto' not allowed in function return type
        auto c_str() const -> char const*;
        ^~~~
./catch.hpp:559:27: error: expected ';' at end of declaration list
        auto c_str() const -> char const*;
                          ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

In short, the Catch2 source code itself is generating several of the same syntax error. I suspect that this might have to do with the 'version' of C++ Catch is written for is different from the one my compiler expects, but I wasn't able to quickly determine whether this was the issue from a Google search for this error.

Here are the details of my g++ compiler:

$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Any idea what is causing this error, and how I could get the Catch2 unit tests to work?

like image 396
Kurt Peek Avatar asked Oct 14 '18 23:10

Kurt Peek


People also ask

Why an error expected declaration or statement at end of input?

Here, we will learn why an error expected declaration or statement at end of input is occurred and how to fix it? The main cause of this error is – missing closing curly brace ( }) of the main () block. How to fix? To fix this and such errors, please take care of curly braces, they are properly opened and closed.

Why am I getting syntax errors in catch2?

In short, the Catch2 source code itself is generating several of the same syntax error. I suspect that this might have to do with the 'version' of C++ Catch is written for is different from the one my compiler expects, but I wasn't able to quickly determine whether this was the issue from a Google search for this error.

What is catch2 in C++ testing?

In my book, Modern C++ Programming Cookbook, I discussed several testing frameworks for C++, more precisely, Boost.Test, Google Test, and Catch (which stands for C++ Automated Test Cases in a Header ). Since the publishing of the book, a new version of Catch, called Catch2 has been released.

How to make compiler use C++11?

Apparently, you need to specify that the compiler should use C++11: In short, with the -std=c++11 option it works. Thanks for contributing an answer to Stack Overflow!


1 Answers

I found the answer at https://github.com/catchorg/Catch2/issues/487. Apparently, you need to specify that the compiler should use C++11:

$ g++ -std=c++11 BinarySearch.cpp
$ ./a.out
===============================================================================
All tests passed (1 assertion in 1 test case)

In short, with the -std=c++11 option it works.

like image 172
Kurt Peek Avatar answered Sep 30 '22 09:09

Kurt Peek