【发布时间】:2014-05-05 17:40:46
【问题描述】:
我正在尝试在某个范围内查找项目,因此我对名为“find”的模板化函数进行了多项测试。
template <typename T> T* find(T *left, T *end, T item);
这是我正在使用的函数原型,它无法与我的第一个测试一起使用:
static void TestFind1(void)
{
cout << "***** Find1 *****" << endl;
const int i1[] = {-1, 2, 6, -1, 9, 5, 7, -1, -1, 8, -1};
int size = sizeof(i1) / sizeof(int);
const int *end = i1 + size;
CS170::display(i1, end);
const int item = 9;
const int *pos = CS170::find(i1, end, item);
if (pos != end)
cout << "Item " << item << " is " << *pos << endl;
else
cout << "Item " << item << " was not found" << endl;
}
上面写着@const int *pos“错误:没有函数模板的实例”find”匹配参数列表参数类型是(const int [11], const int *, const int)”
我有第二个原型可以与这个测试一起工作,但它没有完全模板化,所以它失败了第二个测试,它要求一个 int *pos 而不是一个 const int *pos。
第二个原型:
template <typename T> const int* find(T *left, T *end, const int item);
我不太确定我应该如何模板化第一个函数来处理任何情况。
【问题讨论】:
标签: c++ templates pointers syntax-error function-templates