user2961927
user2961927

Reputation: 1718

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

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. 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!

Upvotes: 0

Views: 113

Answers (1)

zdan
zdan

Reputation: 29450

It looks to me like this line in 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:

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

Upvotes: 2

Related Questions