模板化类中的模板函数

template function in a templated class

本文关键字:函数      更新时间:2023-10-16

我有一个模板化类,我想在该类中使用创建一个模板化函数。 我现在似乎想不通该怎么做。

我把它归结为一个简单的程序:

#include <iostream>
template<typename TInputType = short,
typename TInternalType = float>
class MyClass
{
public:
    void Print();
    template<typename TAnotherType> void DoSomething(TAnotherType t);
};
template<typename TInputType, typename TInternalType>
void MyClass<TInputType,TInternalType>::Print()
{
    printf("whats upn");
}
template<typename TInputType, typename TInternalType, typename TAnotherType>
void MyClass<TInputType,TInternalType>::DoSomething(TAnotherType t)
{
    std::cout << "whats up:" << t << std::endl;
}
int main() {
    MyClass<> tst;
    tst.Print();
    tst.DoSomething<int>(10);
    std::cout << "!!!Hello World!!!" << std::endl;
    return 0;
}

我收到错误:无效使用不完整的类型或错误:模板重新声明中的模板参数过多

好的,所以...我一直在试验,我想通了。您需要两个模板调用

...
template<typename TInputType, typename TInternalType>
template<typename TAnotherType>
void MyClass<TInputType,TInternalType>::DoSomething(TAnotherType t)
{
    std::cout << "whats up:" << t << std::endl;
}
...