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

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

    You can do it in this way

    // xyz.h
    #ifndef _XYZ_
    #define _XYZ_
    
    template 
    class XYZ {
      //Class members declaration
    };
    
    #include "xyz.cpp"
    #endif
    
    //xyz.cpp
    #ifdef _XYZ_
    //Class definition goes here
    
    #endif
    

    This has been discussed in Daniweb

    Also in FAQ but using C++ export keyword.

提交回复
热议问题