【发布时间】:2025-12-04 10:00:01
【问题描述】:
我正在阅读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?
请帮帮我。
【问题讨论】:
-
您是否启用 C++11? GCC C++ 编译器通常需要一个特殊的标志 (
-std=c++11)。 -
@为什么对这个问题投反对票?是什么原因?
-
那么 Code::Blocks 使用什么编译器呢?什么版本的?
-
GCC 直到 4.9 版才真正获得完整的 C++11 功能,可能是您的版本不支持该异常。
-
为什么对这个问题投反对票?这个问题有什么问题?
标签: c++ c++11 exception-handling compiler-errors