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

Why did this C++ program fail in compilation?

我正在阅读this. I tested this program on code blocks 13.12 IDE which supports C++11 but it is getting failed in compilation & compiler shows multiple errors. Look at the program. It works fine on online compiler see 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'

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

请帮帮我。

你的编译器不支持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. According to this mailing list post,甚至trunk GCC自2013年以来仅支持std::bad_array_new_length

通过将 GCC 参考手册一分为二,我们可以确定 GCC 4.8.4 doesn't have it but GCC 4.9.2 does。您链接到的 "online compiler" 运行 GCC 4.9.2。

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

"C++11 support" 是一个非常广泛的术语,您会发现,直到最近,它基本上 never 意味着 complete C++11 支持。例如,C++11 regexes weren't supported at all until GCC 4.9,或者。

如果您确实在使用 gcc 4.7.1(从 ) looking at the GCC 4.7.1 Standard C++ Library Reference Manual you can see that according to the api doc 可以了解到您的 gcc 版本没有 bad_array_new_length class.