jmerkow
jmerkow

Reputation: 1860

template function in a templated class

I have a templated class and I want to use a create a templated function inside that class. I can't seem to figure out now to do it.

I boiled it down to a simple program:

#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;
}

I get error: invalid use of incomplete type OR error: too many template parameters in template redeclaration

Upvotes: 1

Views: 124

Answers (1)

jmerkow
jmerkow

Reputation: 1860

Ok so... Ive been experimenting and I figured it out. You need two template calls

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

Upvotes: 3

Related Questions