【问题标题】:Passing a std::pair templated with two template arguments as a parameter [closed]传递带有两个模板参数的模板化 std::pair 作为参数[关闭]
【发布时间】:2016-05-03 18:34:10
【问题描述】:

我正在编写一个函数 insertPairIntoVector,它将根据对中第二个元素的比较将 std::pair 插入到 std::vector 中。我的原型是这样的:

template <typename T, typename Q>
std::vector<std::pair<T, Q>> sortPairIntoVector(
    std::pair<T,Q>, std::vector<std::pair<T, Q>>);

我的函数定义是这样的:

template <typename T, typename Q>
std::vector<std::pair<T, Q>> sortPairIntoVector(
    std::pair<T, Q>, std::vector<std::pair<T, Q>>) {

}

但我仍然收到大量关于模板的模棱两可的错误。关于我做错了什么有什么帮助吗? 谢谢!

【问题讨论】:

  • Any help as to what I am doing wrong? 对于初学者,没有显示任何调用该函数的代码,也没有显示您声称看到的任何错误消息。我们不是千里眼。我强烈建议你创建一个MCVE

标签: c++ templates c++11


【解决方案1】:

当两个“>”字符彼此靠近时,它们之间需要一个空格,这样它就不会被解释为“>>”运算符。完成该更改后,原型编译。

#include <iostream>
#include <vector>
#include <iostream>

template <typename T, typename Q>
std::vector<std::pair<T, Q> > sortPairIntoVector(
    std::pair<T,Q>, std::vector<std::pair<T, Q> >);
template <typename T, typename Q>
std::vector<std::pair<T, Q> > sortPairIntoVector(
    std::pair<T, Q>, std::vector<std::pair<T, Q> >) {

}

int main() {
}

【讨论】:

  • 这已在 C++11 中得到修复——这是问题的具体标记。
  • 这被标记为 c++11 所以这不应该是一个问题。
猜你喜欢
  • 1970-01-01
  • 2020-06-07
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
相关资源
最近更新 更多