Splitting templated C++ classes into .hpp/.cpp files--is it possible?

前端 未结 16 1221
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 17:27

I am getting errors trying to compile a C++ template class which is split between a .hpp and .cpp file:

$ g++ -c -o main.o main.cpp         


        
16条回答
  •  执念已碎
    2020-11-22 17:45

    It is possible, as long as you know what instantiations you are going to need.

    Add the following code at the end of stack.cpp and it'll work :

    template class stack;
    

    All non-template methods of stack will be instantiated, and linking step will work fine.

提交回复
热议问题