简体   繁体   English

使用函数指针时C ++模板编译错误

[英]c++ template compiling error when using function pointers

I am getting compiling errors for the following code: 我收到以下代码的编译错误:

Define a new type function pointer, which I have to wrap it with a struct for some semantic reason: 定义一个新的类型函数指针,出于某种语义原因,我必须使用结构将其包装:

template<typename T>
struct sparseDistance{
    typedef float (*sparseDistanceCore)(typename std::vector<colcell<T> >::iterator, 
        typename std::vector<colcell<T> >::iterator, unsigned, unsigned, unsigned, T);
};

The following function is to reference the 3 functions: sparseDistanceEuc, sparseDistanceCheb and sparseDistanceCB 以下函数引用了这三个函数:sparseDistanceEuc,sparseDistanceCheb和sparseDistanceCB

template<typename T>
typename sparseDistance<T>::sparseDistanceCore getDistanceFun(std::string& ms)
{
  if(ms=="Euc")return &sparseDistanceEuc;
  else if(ms=="Cheb")return &sparseDistanceCheb;
  return &sparseDistanceCB;
}

In the main event function's definition: 在主事件函数的定义中:

template<typename T>
std::vector<float> sparsePairColDInternal(std::vector<std::vector<colcell<T> > >&M, 
    std::string&metric, typename T specialValue=0){
typename sparseDistance<T>::sparseDistanceCore D=
    getDistanceFun<typename sparseDistance<T>::sparseDistanceCore>(metric);
//...Do the rest steps
}

In the main() function, I initialized the type(T) as double. 在main()函数中,我将type(T)初始化为double。 The compiler then give the following error: 然后,编译器给出以下错误:

distanceFuntion.cpp(187): error C2440: 'initializing' : cannot convert from     'float (__cdecl *)    (std::_Vector_iterator<_Myvec>,std::_Vector_iterator<_Myvec>,unsigned int,unsigned int,unsigned int,T)' to 'float (__cdecl *)(std::_Vector_iterator<_Myvec>,std::_Vector_iterator<_Myvec>,unsigned int,unsigned int,unsigned int,T)'
      with
      [
          _Myvec=std::_Vector_val<std::_Simple_types<colcell<float (__cdecl *)(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<colcell<double>>>>,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<colcell<double>>>>,unsigned int,unsigned int,unsigned int,double)>>>,
          T=float (__cdecl *)(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<colcell<double>>>>,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<colcell<double>>>>,unsigned int,unsigned int,unsigned int,double)
      ]
      and
      [
          _Myvec=std::_Vector_val<std::_Simple_types<colcell<double>>>,
          T=double
      ]
      This conversion requires a reinterpret_cast, a C-style cast or function-style cast
      distanceFuntion.cpp(234) : see reference to function template instantiation 'std::vector<_Ty> sparsePairColDInternal<T>(std::vector<std::vector<colcell<T>>> &,std::string &,T)' being compiled
      with
      [
          _Ty=float,
          T=double
      ]
      distanceFuntion.cpp(324) : see reference to function template instantiation 'std::vector<_Ty> sparsePairColDNoneBin<double>(std::vector<sparseColumn<T>> &,std::string &,T)' being compiled
      with
      [
          _Ty=float,
          T=double
      ]

Can anybody help? 有人可以帮忙吗?

Thanks! 谢谢!

It looks to me like this line in sparsePairColDInternal : 在我看来,这就像sparsePairColDInternal这一行:

typename sparseDistance<T>::sparseDistanceCore D=
getDistanceFun<typename sparseDistance<T>::sparseDistanceCore>(metric);

should be: 应该:

typename sparseDistance<T>::sparseDistanceCore D=
getDistanceFun<T>(metric);

otherwise the type of getDistanceFun looks like this: 否则, getDistanceFun的类型如下所示:

typename sparseDistance<sparseDistance<T>::sparseDistanceCore>::sparseDistanceCore getDistanceFun(std::string& ms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM