Dynamic Allocation in Template Class Constructor

谁说我不能喝 提交于 2019-12-06 15:46:33

It's not a runtime error, it's a linker error. The problem is probably that the implementations of the constructor and the destructor are in a source file. With template classes, you have to place the implementations of all the methods in the header (or in the source file that uses them, but that's equivalent to putting them in the header).

So basically do this:

template<class T>
class stack
{
public:
    stack( const int n)
    {
        capacity = n ;
        size = 0 ;
        arr = new T [capacity] ;
    }

    // and the same for all other method implementations
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!