我在ubuntu 64 bit 14.04下使用g ++(gcc)版本4.9.2。 我正在尝试编译以下简单代码:
#include <iostream>
#include <atomic>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
并收到以下错误:
/usr/include/c++/4.9/atomic:385:56: error: ‘noexcept’ does not name a type
memory_order __m = memory_order_seq_cst) volatile noexcept
^
/usr/include/c++/4.9/atomic:389:53: error: expected ‘;’ at end of member declaration
load(memory_order __m = memory_order_seq_cst) const noexcept
^
/usr/include/c++/4.9/atomic:389:59: error: ‘noexcept’ does not name a type
load(memory_order __m = memory_order_seq_cst) const noexcept
^
/usr/include/c++/4.9/atomic:393:59: error: expected ‘;’ at end of member declaration
load(memory_order __m = memory_order_seq_cst) const volatile noexcept
^
/usr/include/c++/4.9/atomic:393:68: error: ‘noexcept’ does not name a type
load(memory_order __m = memory_order_seq_cst) const volatile noexcept
^
/usr/include/c++/4.9/atomic:398:48: error: expected ‘;’ at end of member declaration
memory_order __m = memory_order_seq_cst) noexcept
^
/usr/include/c++/4.9/atomic:398:50: error: ‘noexcept’ does not name a type
memory_order __m = memory_order_seq_cst) noexcept
为什么我不能编译这个简单的例子?
答案 0 :(得分:9)
查看第一个错误,而不是最后一个错误。以后的错误通常是由较早的错误引起的,所以只查看最后的错误是浪费时间,并没有告诉你根本原因。
第一个错误是:
在/usr/include/c++/4.9/atomic:38:0中包含的文件中, 来自a.cc:2: /usr/include/c++/4.9/bits/c++0x_warning.h:32:2:错误:#error此文件需要ISO C ++ 2011标准的编译器和库支持。此支持目前是实验性的,必须使用-std = c ++ 11或-std = gnu ++ 11编译器选项启用。
这应该是非常明显的。您没有告诉编译器您想要使用C ++ 11。
它甚至会告诉您如何解决问题:使用-std=c++11
或-std=gnu++11
进行编译
N.B。对于GCC 5,<atomic>
标题已被修改,以便您仅获得一个错误,而不是之后产生的所有后果。