【问题标题】:why wont tuples work? [closed]为什么元组不起作用? [关闭]
【发布时间】:2016-09-02 19:35:58
【问题描述】:

我正在尝试使用 c++ 类元组编写一个简单的程序,但是,我可以让它工作,我尝试通过 g++ -std=gnu++0x tuple.cpp 编译返回的东西就像元组是不完整的类型,它不识别函数 get()。 这是我的代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <tuple> 

using namespace std;

int main(void) {

tuple<int, int> tup;

get<1>(tup) = 1;
get<2>(tup) = 2;

cout << "tup1:" << get<1>(tup) << "tup2:" << get<2>(tup) << endl;


return 0;


}

【问题讨论】:

  • 就像 C++ 中的其他一切都使用基于 0 的索引一样,您的元组有 2 个元素(idxs 0 和 1,not 1 和 2)
  • 始终粘贴完整的错误消息。
  • "c++ 类元组" -- tuple 不是一个类;这是一个模板。两者本质上是不同的,如果你混淆了它们的含义,你将遭受无穷无尽的痛苦。

标签: c++ tuples


【解决方案1】:

它无法识别函数get()

这是一个编译时错误,暗示元组元素索引无效。

元组索引从 0 开始,因此 2 个元素的元组具有索引 0 和 1。

tuple<int, int> tup;
get<2>(tup) ...; // <--- index 2 is not valid

修复:

get<0>(tup) = 1;
get<1>(tup) = 2;

【讨论】:

  • 更改索引帮助谢谢!!
猜你喜欢
  • 2018-02-15
  • 2018-02-15
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多