Function template in non-template class

后端 未结 1 641
深忆病人
深忆病人 2021-02-04 05:50

I\'m sure that it is possible but I just can\'t do it, which is: How can I define function template inside non-template class? I tryied something like this:

clas         


        
相关标签:
1条回答
  • 2021-02-04 06:13

    Don't put the <T> after the function name. This should work:

    template<class T>
    void Stack_T::put(const T& obj)
    {
    }
    

    This still won't work if the function definition is not in the header file. To solve this, use one of:

    • Put the function definition in the header file, inside the class.
    • Put the function definition in the header file after the class (like in your example code).
    • Use explicit template instanciation in the header file. This has serious limitations though (you have to know all possible values of T in advance).
    0 讨论(0)
提交回复
热议问题