【发布时间】:2011-03-18 07:33:07
【问题描述】:
我有这个问题:
template<typename T> class Bubu
{
...
int (*comparer)(const T t1, const T t2);
...
public:
Bubu(int (*_comparer)(const T t1, const T t2))
{
comparer = _comparer;
}
};
在另一个文件中:
Bubu<char*> asd(strcmp);
错误:
error C2664: 'Bubu<T>::Bubu(int (__cdecl *)(const T,const T))' :
cannot convert parameter 1 from 'int (__cdecl *)(const char *,
const char *)' to 'int (__cdecl *)(const T,const T)'
我不明白为什么。编译器不应该在那里看到“char*”而不是“T”吗?
编辑:Ideone.com-ready 代码:
int asdf(const char* a, const char* b)
{ return 0; }
template class Bubu
{
int (*comparer)(const T t1, const T t2);
public:
Bubu(int (*_comparer)(const T t1, const T t2))
{
comparer = _comparer;
}
};
int main(int argc, char* argv[])
{
Bubu asd(asdf);
}
【问题讨论】:
-
一定是错字:
int (*comparer)() -
请整理一下!当我尝试编译它时,我遇到了大约五个不相关的错误。然后我放弃了。给我们一段代码,我们可以通过 ideone.com 发布!
标签: c++ templates function-pointers