模板函数重载错误C2688
overloading of template function error C2688
我无法编译以下代码
namespace sequential_sort
{
template<class T>
void sort(std::list<T>& source)
{
sort(source.begin(), source.end()); //(1)
}
template<class Iter>
void sort(Iter begin, Iter end)
{
if(begin == end)
return;
typedef Iter::value_type value_type;
value_type value = *(begin);
Iter part = std::partition(begin, end, [&value](const value_type&->bool{return t < value;});
sort(begin, part);
Iter divide = part;
divide++;
sort(divide, end);
}
}
它说,在第(1)行,我有错误C2688,对重载函数的调用不明确。我不明白为什么重载的函数甚至有不同数量的参数?
有几个问题在起作用:
1) 您需要在双参数函数之前声明您的单参数sequential_sort::sort
函数,除非您想要另一个sort
函数。你可能看不到它的效果,因为你使用的是Windows C++编译器,它在这方面不符合标准。应该发生的是,std::sort
是明确选择的,这不会编译,因为它需要随机访问迭代器(std::list
有一个sort
成员函数,应该使用它来代替std::sort
)。
2) 一旦修复,由于要将std::list
迭代器传递给两个参数的sort
,依赖于参数的查找(ADL)意味着也要考虑std::sort
。这就是造成歧义的原因。您可以通过具体说明要使用哪两个参数sort
来解决此问题:
#include <list>
#include <algorithm>
namespace sequential_sort
{
template<class Iter>
void sort(Iter begin, Iter end) { }
template<class T>
void sort(std::list<T>& source)
{
sequential_sort::sort(source.begin(), source.end()); //(1)
//^^^^^^^^^^^^^^^
}
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- visual MSVC编译器错误C2688:Microsoft C++ABI角大小写问题
- 模板函数重载错误C2688