2013-06-27 139 views
0

我不能编译下面的代码重载模板函数错误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

难道你的名字不是'sort'其他的功能的东西:sort你想用你可通过特定的大约两个参数解决这个问题?有'std :: sort'和参数相关的查找要考虑。 – juanchopanza

回答

0

那里玩几个问题:

1)你必须两个参数一个之前来声明单个参数sequential_sort::sort功能,除非你想另一个sort功能。您可能不会看到这种效果,因为您使用的是Windows C++编译器,在这方面它不符合标准。本来应该发生的是std::sort被明确挑选出来,并且因为它需要随机访问迭代器(std::list有一个sort成员函数而不是std::sort),所以无法编译。

2)一旦被固定,由于要传递std::list迭代器的两个参数sortargument dependent lookup(ADL)指std::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) 
//^^^^^^^^^^^^^^^ 
} 

} 
+0

谢谢。它工作正常。 –