c++模板给出了无法解析的函数类型
C++ Template gives unresolved function type
我正在尝试制作一个模板,比较第二项对:
template <class T>
bool sortPairKey2(T u, T v) { return u.second < v.second;}
这样,如果我有一对对,我仍然可以使用这个函数
但是当我在sort方法(from)中使用它时,我得到:
error: no matching function for call to ‘sort(std::vector<std::pair<int, int> >::iterator, std::vector<std::pair<int, int> >::iterator, <unresolved overloaded function type>)’
sort(sums.begin(), sums.end(), sortPairKey2<iipair,iipair>);
^
stp2mzn.cpp:409:63: note: candidates are:
In file included from /usr/include/c++/4.8/algorithm:62:0,
from stp2mzn.cpp:9:
/usr/include/c++/4.8/bits/stl_algo.h:5447:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
^
/usr/include/c++/4.8/bits/stl_algo.h:5447:5: note: template argument deduction/substitution failed:
stp2mzn.cpp:409:63: note: candidate expects 2 arguments, 3 provided
sort(sums.begin(), sums.end(), sortPairKey2<iipair,iipair>);
^
In file included from /usr/include/c++/4.8/algorithm:62:0,
from stp2mzn.cpp:9:
/usr/include/c++/4.8/bits/stl_algo.h:5483:5: note: template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)
sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
^
/usr/include/c++/4.8/bits/stl_algo.h:5483:5: note: template argument deduction/substitution failed:
stp2mzn.cpp:409:63: note: couldn't deduce template parameter ‘_Compare’
sort(sums.begin(), sums.end(), sortPairKey2<iipair,iipair>);
其中typedef pair<int,int> iipair
.
函数工作良好时,而不是T
我有pair<int,int>
。我看不出有什么问题。
谢谢!
请尝试sortPairKey2<iipair>
代替sortPairKey2<iipair,iipair>
两件事。
- 你应该通过const引用 传递参数
- 不写
sortPairKey2<iipair, iipair>
,要么完全摆脱模板规范(你的编译器应该找到它),要么直接写sortPairKey2<iipair>
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 是否有任何建议来统一函数类型限定符并简化可恶的函数类型?
- 关于 C++ 中的函数类型定义
- 用于检测函数类型是否为否的特征
- 函数类型参数的模板参数推导
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++无效的函数类型转换
- STL 函数和函数类型与函数指针类型
- 如何将result_of与函数类型定义一起使用
- 将模板(没有规范)传递给 std::thread() 会出现错误:<未解析的重载函数类型>匹配错误
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- 专门用于"direct"函数类型(与函数指针类型相对)
- 将函数类型作为模板参数传递不会编译
- 通过参数传递 lambda(无函数类型模板)
- 如何在模板参数中分离函数类型返回类型和参数
- 为什么比较函数类型需要指定为模板参数?
- 带有限定符的函数类型定义用例
- 如何声明对函数类型的常量引用
- 非类型模板参数允许各种函数类型?