-4

我想编写一个模板函数,它接受模板类作为输入参数。

template<class T>
void Function(T Input)
{

}

类似上面的东西。该类可以是模板类吗?如果是,我该怎么写?我尝试执行此操作时出错。请帮助我。提前致谢

编辑:

 template<class T>
    void ReadImage(T InputImage)
    {
        InputImage::Pointer InputImagePointer = InputImage::New();
        typedef itk::ImageFileReader<ImageType> ReaderType;
        ReaderType::Pointer ImageFileReader = ReaderType::New();

        ImageFileReader->SetFileName(FileName);
        ImageFileReader->Update();

    }

        int main()
        {
        std::string FileName = "NameOfTheFile.mhd";
            std::ifstream InputFile(FileName, ios_base::in);
        while (!InputFile.eof())
            {
                string InputData;
                //getline(InputFile, InputData);
                InputFile >> InputData;
                if (InputData == "ElementType")
                {
                    //cout << "Came Here" << endl;
                    InputFile >> InputData;
                    InputFile >> InputData;
                    if (InputData == "MET_UCHAR")
                    {
    typedef unsigned char ImagePixelType;
                    typedef itk::Image<ImagePixelType, 3> InputImageType;
    ReadImage(InputImageType);

    }
    else if(InputData == "MET_USHORT")
    {
    typedef unsigned char ImagePixelType;
                    typedef itk::Image<ImagePixelType, 3> InputImageType;
    ReadImage(InputImageType);
    }
        }
        }

我收到以下错误:错误 1 ​​错误 C2275:'ImageType':非法使用此类型作为表达式

当我在 if else 条件下调用 ReadImage() 函数时发生错误

4

1 回答 1

0

您应该将类​​型作为模板参数传递给模板函数。

像这样的代码:

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

namespace itk
{
    template<class T, int n>
    class Image
    {
    public:
        typedef Image*  Pointer;

        static Pointer New()
        {
            return new Image;
        }
    };

    template<class T>
    class ImageFileReader
    {
    public:
        typedef ImageFileReader*    Pointer;

        static Pointer New()
        {
            return new ImageFileReader;
        }

        void SetFileName(const std::string& fileName)
        {
        }

        void Update()
        {
        }
    };
}

template<class InputImageType>
void ReadImage(const std::string& fileName)
{
    typename InputImageType::Pointer inputImagePointer = InputImageType::New();
    typedef itk::ImageFileReader<InputImageType>    ReaderType;
    ReaderType::Pointer ImageFileReader = ReaderType::New();

    ImageFileReader->SetFileName(fileName);
    ImageFileReader->Update();
}

int main()
{
    std::string fileName = "NameOfTheFile.mhd";
    std::ifstream inputFile(fileName, ios_base::in);
    while (!inputFile.eof())
    {
        string InputData;
        inputFile >> InputData;
        if (InputData == "ElementType")
        {
            //cout << "Came Here" << endl;
            inputFile >> InputData;
            inputFile >> InputData;
            if (InputData == "MET_UCHAR")
            {
                typedef unsigned char ImagePixelType;
                typedef itk::Image<ImagePixelType, 3> InputImageType;
                ReadImage<InputImageType>(fileName);

            }
            else if (InputData == "MET_USHORT")
            {
                typedef unsigned char ImagePixelType;
                typedef itk::Image<ImagePixelType, 3> InputImageType;
                ReadImage<InputImageType>(fileName);
            }
        }
    }
}
于 2014-12-20T10:55:55.040 回答