模板化类中的模板函数

时间:2014-12-03 22:57:51

标签: c++ templates

我有一个模板化的类,我想在该类中使用创建模板化的函数。我现在似乎无法理解这一点。

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

#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 up\n");
}
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;
}

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

1 个答案:

答案 0 :(得分:3)

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

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