【问题标题】:Compile error "error: expected ';' at end of declaration list" using Catch testing framework in C++编译错误“错误:预期的';'在声明列表末尾”使用 C++ 中的 Catch 测试框架
【发布时间】:2018-10-14 23:03:32
【问题描述】:

我正在尝试通过在其中实现一些简单的算法来学习 C++。为了测试这些算法,我想使用Catch2。这是我为二进制搜索提出的程序:

#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);
}

我已将 catch.hpp 单个头文件复制到同一目录中。问题是当我尝试在我的 Mac 上使用 g++ 命令编译它时,我收到以下错误:

$ 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.

简而言之,Catch2 源代码本身会产生几个相同的语法错误。我怀疑这可能与编写的 C++ Catch 的“版本”与我的编译器期望的不同有关,但我无法通过 Google 搜索此错误快速确定这是否是问题。

以下是我的g++ 编译器的详细信息:

$ 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

知道是什么导致了这个错误,以及如何让 Catch2 单元测试工作?

【问题讨论】:

  • 我怀疑第 380 行或附近缺少分号。
  • 你是怎么编译这个的?你给g++什么参数?
  • 我刚刚运行了g++ BinarySearch.cpp,其中BinarySearch.cpp 是文件名(如上图错误消息之前所示)。确实,错误消息表明缺少分号,但我怀疑 Catch2 源代码中是否存在语法错误,至少对于它所针对的 C++ 版本而言不会。

标签: c++ catch-unit-test


【解决方案1】:

我在https://github.com/catchorg/Catch2/issues/487 找到了答案。显然,您需要指定编译器应该使用 C++11:

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

简而言之,使用 -std=c++11 选项可以正常工作。

【讨论】:

  • 您确实是正确的,因为 Catch2 需要 C++11(毕竟,这是描述“用于单元测试、TDD 和 BDD 的现代、C++ 原生、仅标头的测试框架- 使用 C++11、C++14、C++17 及更高版本”),但相关问题无关紧要。它可以追溯到 Catch v1,它支持 C++98,但在某些版本中对 nullptr_t 支持的检测失败。
猜你喜欢
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多