gpt4 book ai didi

c++ - 模板推导失败

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:21 25 4
gpt4 key购买 nike

谁能解释为什么这不起作用:

#include "itkCovariantVector.h"
#include "itkImage.h"

template <typename TComponent, int NumberOfComponents>
void FillImage(itk::Image<itk::CovariantVector<TComponent,
NumberOfComponents>, 2>* const image,
itk::Image<itk::CovariantVector<TComponent,
NumberOfComponents>, 2>* const output)
{
std::cout << "Works." << std::endl;
}

int main(int, char* [] )
{
typedef itk::Image<itk::CovariantVector<float, 3u>, 2u> ImageType;

ImageType::Pointer imageSmartPointer = ImageType::New();
ImageType* image = imageSmartPointer.GetPointer();
FillImage(image, image);
return 0;
}
/*
no matching function for call to ‘FillImage(ImageType*&, ImageType*&)’
note: candidate is:
template<class TComponent, int NumberOfComponents> void
FillImage(itk::Image<itk::CovariantVector<TComponent,
NumberOfComponents>, 2u>*, itk::Image<itk::CovariantVector<TComponent,
NumberOfComponents>, 2u>*)
*/

这些类模板的定义是:http://www.itk.org/Doxygen/html/classitk_1_1CovariantVector.htmlhttp://www.itk.org/Doxygen/html/classitk_1_1Image.html

我用非 ITK 类创建了相同的情况,并且它工作正常:

#include <iostream>

template <typename TPixel, int Dimensions>
struct Image
{
};

template <typename TComponent, int NumberOfComponents>
struct Vector
{
};

template <typename TComponent, int NumberOfComponents>
void FillImage(Image<Vector<TComponent, NumberOfComponents>, 2 >* const image,
Image<Vector<TComponent, NumberOfComponents>, 2 >* const output)
{
std::cout << "Works." << std::endl;
}

int main(int, char* [] )
{
typedef Image<Vector<float, 3>, 2 > ImageType;

ImageType* image = new ImageType;
FillImage(image, image);
delete image;
return 0;
}

谁能解释一下可能有什么不同?

最佳答案

问题是我必须更改 <int NumberOfComponents> 的签名至 <unsigned int NumberOfComponents>所以它符合 ITK 类的定义。

#include <iostream>

template <typename TPixel, int Dimensions>
struct Image
{
};

// The deduction works with this
// template <typename TComponent, int NumberOfComponents>
// struct Vector
// {
// };

// The deduction does NOT work with this (unsigned int vs int above)
template <typename TComponent, unsigned int NumberOfComponents>
struct Vector
{
};

template <typename TComponent, int NumberOfComponents>
void FillImage(Image<Vector<TComponent, NumberOfComponents>, 2 >* const image,
Image<Vector<TComponent, NumberOfComponents>, 2 >* const output)
{
std::cout << "Works." << std::endl;
}

int main(int, char* [] )
{
typedef Image<Vector<float, 3>, 2 > ImageType;

ImageType* image = new ImageType;
FillImage(image, image);
return 0;
}

关于c++ - 模板推导失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com