我有这样的问题:C++函数指针模板内
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就绪代码:
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(*比较器)()' – grep 2011-03-18 07:46:52
整理这件事,请!当我试图编译它时,我得到了大约五个无关的错误。然后我放弃了。给我们一个代码块,我们可以通过ideone.com! – TonyK 2011-03-18 07:50:42
@grep:我不明白 – cantrem 2011-03-18 07:50:59