C 错误:呼叫函数模板没有匹配功能

C++ error: no matching function for call to function template

本文关键字:功能 函数模板 错误 呼叫      更新时间:2023-10-16

我有一个功能模板,该模板从图像中提取数据并将其复制到较小的数组(我称为 Patch),模板函数称为 copyPatch。它被定义为:

template <class DestType, class SrcType, class Transformation>
bool copyPatch(Patch<DestType> &patch, 
               ImageData<SrcType>* src_data, 
               size_t src_ul_pix, 
               size_t src_ul_line)

注意:Transformation参数允许我通过在数据上执行某些转换的类中传递。我将模板函数称为如下,

copyPatch<float, uint8_t, StraightCopy>(m_patch_data, m_data.t8u,
                                        ul_pix, ul_line)

其中m_patch_data是类型Patch<float>m_data.t8u是定义如下的联合的成员:

union {
    ImageData<uint8_t>*     t8u;
    ImageData<uint16_t>*    t16u;
    ImageData<int16_t>*     t16s;
    ImageData<uint32_t>*    t32u;
    ImageData<int32_t>*     t32s;
    // A bunch more of these
    void*               tvoid;
} m_data;

当我编译此问题时,我会收到以下错误(我已经进行了一些篡改):

error: no matching function for call to:
copyPatch(Patch<float>&, ImageData<unsigned char>*&, size_t&, size_t&)’
copyPatch<float, uint8_t, StraightCopy>( m_patch_data, m_data.t8u, ul_pix, ul_line);
                                                                                          ^
note: candidate is:
template<class DestType, class SrcType, class Transformation> 
bool copyPatch(Patch<T>&, ImageData<SrcType>*, size_t, size_t)
template argument deduction/substitution failed:

对我来说,我不明白为什么功能不匹配。我能看到的唯一可能的原因是,对于需要指针的第二个参数(这是我认为我正在传递的内容),但是调用代码似乎正在传达对指针的引用。

编译器为G 4.8.1。

正如评论中指出的那样,我的转换(直接拷贝)的问题定义如下:

template<class Dest, class Src>
class StraightCopy {
public:
    Dest operator()(Src s) { return static_cast<Dest>(s); } 
};

我错过了将参数传递给我的直型类。

感谢plasmahh将我指向正确的方向。我的转换类型(直接拷贝)需要参数。所以我的电话看起来像:

copyPatch<float, uint8_t, StraightCopy< float, uint8_t > >( m_patch_data, m_data.t8u, ul_pix, ul_line);

不是那么美丽:o)