[英]ISO C++ forbids declaration of ‘tuple’ with no type
When trying to compile a simple class ( g++ myclass.cpp
), I get the following error: 在尝试编译一个简单的类(
g++ myclass.cpp
)时,我收到以下错误:
ISO C++ forbids declaration of 'tuple' with no type
ISO C ++禁止声明'tuple'没有类型
I searched for this problem, and in most cases people seemed to forget std::
or including <tuple>
in the header. 我搜索了这个问题,在大多数情况下,人们似乎忘记了标题中的
std::
或包含<tuple>
。 But I have both. 但我有两个。 Here is my code:
这是我的代码:
myclass.h myclass.h
#ifndef MYCLASS
#define MYCLASS
#include <iostream>
#include <tuple>
class MyClass {
std::tuple<bool, int, int> my_method();
};
#endif
myclass.cpp myclass.cpp
#include "myclass.h"
using namespace std;
tuple<bool, int, int> MyClass::my_method() {
return make_tuple(true, 1, 1);
}
If I do the same using pair
instead, leaving out the second int
and including <set>
, it works. 如果我使用
pair
相同,省略第二个int
并包括<set>
,它可以工作。
What am I missing? 我错过了什么?
EDIT: 编辑:
Here is the full output: 这是完整的输出:
$ g++ myclass.cpp -o prog
$ g ++ myclass.cpp -o prog
In file included from myclass.cpp:1:在myclass.cpp中包含的文件中:1:
myclass.h:7: error: ISO C++ forbids declaration of 'tuple' with no typemyclass.h:7:错误:ISO C ++禁止声明'tuple'没有类型
myclass.h:7: error: invalid use of '::'myclass.h:7:错误:无效使用'::'
myclass.h:7: error: expected ';'myclass.h:7:错误:预期';' before '<' token
在'<'标记之前
myclass.cpp:5: error: expected constructor, destructor, or type conversion before '<' tokenmyclass.cpp:5:错误:在'<'标记之前的预期构造函数,析构函数或类型转换
$ g++ --version
$ g ++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658)i686-apple-darwin11-llvm-g ++ - 4.2(GCC)4.2.1(基于Apple Inc. build 5658)
(LLVM build 2336.11.00)(LLVM build 2336.11.00)
GCC 4.2.1 shipped with every mac is outdated. 每个mac附带的GCC 4.2.1已过时。 It will not recognize the C++11.
它无法识别C ++ 11。
You need to compile your code using: c++ instead of g++ which calls clang, which is the officially updated compiler on mac. 您需要使用以下代码编译代码:c ++而不是调用clang的g ++,这是mac上正式更新的编译器。
c++ -std=c++11 -stdlib=libc++ myclass.cpp -o prog
You are required to link against libc++ which is clang lib which knows about c++11 features instead of the default libstdc++ used by gcc. 您需要链接libc ++,它是clang lib,它知道c ++ 11的功能,而不是gcc使用的默认libstdc ++。
Update! 更新! We're on GCC 4.7 these days.
我们这些天正在使用GCC 4.7。
GCC 4.2.1 is from all the way back on 18th July, 2007 . GCC 4.2.1始于2007年7月18日 。 There is only a remote chance that it supports any features from what became C++11.
只有很少的机会它支持C ++ 11中的任何功能。
That said, it may provide some in std::tr1
(ie std::tr1::tuple<T1, T2, ...>
), which is where some of the C++11 features lived in the time before standardisation, though off the top of my head these were introduced to GCC only in 4.4. 也就是说,它可能在
std::tr1
(即std::tr1::tuple<T1, T2, ...>
)中提供一些,这是标准化之前的一些C ++ 11特性所在的地方,虽然我不知道这些仅在4.4中被引入GCC。
With gcc 4.2, tuple
was in namespace std::tr1
. 使用gcc 4.2,
tuple
位于命名空间std::tr1
。 You must include <tr1/tuple>
and specify your method more or less like this 您必须包含
<tr1/tuple>
并指定您的方法或多或少
#ifndef MYCLASS
#define MYCLASS
#include <tr1/tuple>
class MyClass {
std::tr1::tuple<bool, int, int> my_method();
};
#endif
Although, as others already suggested, updating to a more recent gcc might be more appropriate. 虽然正如其他人已经建议的那样,更新到更新的gcc可能更合适。
If you add the -std=c++11
(or, for older versions of g++
the -std=c++0x
) option and add a simicolon after the expression in the member function the code compiles. 如果添加
-std=c++11
(或者,对于旧版本的g++
-std=c++0x
)选项并在成员函数中的表达式之后添加一个simicolon,则代码将进行编译。 If this doesn't work you might have a version which only defines tuple
in namespace std::tr1
(it seems, the implementation provides a <tuple>
header, though, because there is no error about <tuple>
not being found). 如果这不起作用,你可能有一个只定义名称空间
std::tr1
tuple
的版本(看起来,实现提供了一个<tuple>
标题,因为没有找到<tuple>
错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.