c++ - 为什么这个C++程序编译失败?

标签 c++ c++11 exception-handling compiler-errors

我正在阅读 this .我在支持 C++11 的代码块 13.12 IDE 上测试了这个程序,但它在编译中失败并且编译器显示多个错误。看程序。它在在线编译器上运行良好,请参阅 this

// bad_array_new_length example
#include <iostream>     // std::cout
#include <exception>    // std::exception
#include <new>          // std::bad_array_new_length

int main() {
  try {
    int* p = new int[-1];
  } catch (std::bad_array_new_length& e) {
    std::cerr << "bad_array_new_length caught: " << e.what() << '\n';
  } catch (std::exception& e) {   // older compilers may throw other exceptions:
    std::cerr << "some other standard exception caught: " << e.what() << '\n';
  }
}

编译器错误:

7   12      [Error] expected type-specifier

7   37      [Error] expected unqualified-id before '&' token

7   37      [Error] expected ')' before '&' token

7   37      [Error] expected '{' before '&' token

7   39      [Error] 'e' was not declared in this scope

7   40      [Error] expected ';' before ')' token

9   5       [Error] expected primary-expression before 'catch'

9   5       [Error] expected ';' before 'catch'

这里出了什么问题?是编译器错误还是代码块 13.12 IDE 不完全支持 C++11

请帮帮我。

最佳答案

您的编译器不支持 std::bad_array_new_length

code blocks 13.12 的 Google 最高搜索结果是:

The codeblocks-13.12mingw-setup.exe file includes the GCC compiler and GDB debugger from TDM-GCC (version 4.7.1, 32 bit).

GCC 4.7.1 was released in 2012 .根据this mailing list post ,甚至 trunk GCC 自 2013 年以来仅支持 std::bad_array_new_length

通过将 GCC 引用手册一分为二,我们可以确定 GCC 4.8.4 doesn't have it但是GCC 4.9.2 does .您链接到的“在线编译器”运行 GCC 4.9.2。

长话短说,您将需要更新的 GCC。

“C++11 支持”是一个非常宽泛的术语,您会发现,直到最近,它基本上从不意味着完整 C++11 支持.例如,C++11 regexes weren't supported at all until GCC 4.9 , 要么。

关于c++ - 为什么这个C++程序编译失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717277/

相关文章:

python - 在 Python 中捕获所有异常

c++ - 二维点的边界

c++ - 异常的 C++ 链接器错误 - 'Defined in discarded section'

c++ - 来自二维数组的特征图

c++ - 这两个函数调用在内存管理方面的差异?

exception-handling - 在 Clojure 中处理纯度错误?

c++ - 私有(private)继承、友元和异常处理

c++ - 使用 vector 迭代器遇到的错误

C++ 为什么我的模板扩展导致编译器堆栈溢出?

c++ - 未找到 Xcode 的 cmath