尝试编译一个简单的类 ( g++ myclass.cpp
) 时,出现以下错误:
ISO C++ forbids declaration of ‘tuple’ with no type
我搜索了这个问题,大多数情况下人们似乎忘记了std::
或包括 <tuple>
在标题中。但我两者都有。这是我的代码:
myclass.h
#ifndef MYCLASS
#define MYCLASS
#include <iostream>
#include <tuple>
class MyClass {
std::tuple<bool, int, int> my_method();
};
#endif
myclass.cpp
#include "myclass.h"
using namespace std;
tuple<bool, int, int> MyClass::my_method() {
return make_tuple(true, 1, 1);
}
如果我使用 pair
做同样的事情相反,省略了第二个 int
并包括 <set>
, 它有效。
我错过了什么?
编辑:
这是完整的输出:
$ g++ myclass.cpp -o prog
In file included from myclass.cpp:1:
myclass.h:7: error: ISO C++ forbids declaration of ‘tuple’ with no type
myclass.h:7: error: invalid use of ‘::’
myclass.h:7: error: expected ‘;’ before ‘<’ token
myclass.cpp:5: error: expected constructor, destructor, or type conversion before ‘<’ token$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658)
(LLVM build 2336.11.00)
最佳答案
每台 Mac 附带的 GCC 4.2.1 已过时。它不会识别 C++11。
您需要使用以下代码编译您的代码:c++ 而不是调用 clang 的 g++,后者是 mac 上的官方更新编译器。
c++ -std=c++11 -stdlib=libc++ myclass.cpp -o prog
您需要链接 libc++,它是 clang lib,它知道 c++11 的特性,而不是 gcc 使用的默认 libstdc++。
关于c++ - ISO C++ 禁止声明没有类型的 ‘tuple’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14005911/