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

前端 未结 16 1294
被撕碎了的回忆
被撕碎了的回忆 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:34

    Another possibility is to do something like:

    #ifndef _STACK_HPP
    #define _STACK_HPP
    
    template 
    class stack {
        public:
                stack();
                ~stack();
    };
    
    #include "stack.cpp"  // Note the include.  The inclusion
                          // of stack.h in stack.cpp must be 
                          // removed to avoid a circular include.
    
    #endif
    

    I dislike this suggestion as a matter of style, but it may suit you.

提交回复
热议问题