看起来编译器非常接近我想做的事情(因为它调用了我的函数作为候选者),但我不知道我做错了什么。为什么我的函数调用不匹配这个通用函数实现?
#include <stdio.h>
#include <stdlib.h>
#include <list>
using namespace std;
template <class U, template<class U> class T>
void AppendSorted(T<U>& l, U val)
{
typename T<U>::reverse_iterator rt = l.rbegin();
while(((*rt) > val) && (rt != l.rend()))
rt++;
l.insert(rt.base(), val);
}
int main(int argc, char* argv[])
{
list<int> foo;
AppendSorted<int, list<int> >(foo, 5);
list<int>::iterator i;
for(i = foo.begin(); i != foo.end(); i++)
{
printf("%d\n",*i);
}
return 0;
}
我得到的错误是:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:21:43: error: no matching function for call to ‘AppendSorted(std::list<int>&, int)’
test.cpp:21:43: note: candidate is:
test.cpp:8:6: note: template<class U, template<class U> class T> void AppendSorted(T<U>&, U)
你会abbend在初始插件在你AppendSorted,顺便说一句,因为你而()子句中的eval命令是要提领迭代指着rend()当列表为空时。在while()条件中反转表达式以避免这种情况。 – WhozCraig
@WhozCraig:好点。 –