c++ - ISO C++ 禁止声明没有类型的 ‘tuple’

标签 c++ g++ tuples std std-pair

尝试编译一个简单的类 ( 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/

相关文章:

c++ - C++ dyld:未加载库-设置 '-L'后

c++ - 有没有办法扩展和调用 std::functions 的元组?

c++ - 如何在 Qt 中使用 GLEW?

c++ - bWait = false 的 GetOverlappedResults block

c++ - 另一个有序 map 与无序(哈希) map 问题

python - 当顺序很重要时如何从元组列表中删除重复项

python-3.x - 如何在 Python 中将元组中的元素(字符串)转换为整数

c++ - Raytracer 输出非常嘈杂的图像

c++ - 错误 - 编译 g++ 文件时出现 "#include expects "FILENAME"or <FILENAME>"

gcc - 如何在 pip 命令中指定 gcc 路径?