-2

I m really sorry if this question's origin reside in my profound lack of understanding of the template concept itself.

I had defined the following class.

#include <iostream>

template <typename T>
class T3B_data{
public:
    T3B_data<T>();
    ~T3B_data<T>();
    void fromText(std::istream& is);
    void fromText(const std::string& textPath);
    void fromBinary(std::istream& is);
    std::ostream & toText(std::ostream& oStream)const;
    std::ostream & toBinary(std::ostream& oStream);
    friend inline std::ostream& operator << (std::ostream & oStream,const T3B_data<T> & data){
        return data.toText(oStream);
    }
private:
    T * mObj;
};

In order to make a simple template test i tried the following main in order to instantiate it

#include "T3B_data.h"

int main(void){
  T3B_data<int> * myData = new T3B_data<int>();
  return 0;
}

i don t get why did i had to instanciate the template explicitly by adding:

template class T3B_data<int>;

at the end of the header file for the compiler to find the class constructor.

Note :

I m fully aware that appending the line actually force a whole new class generation. i just don t get why the code in the main did not actually force template instanciation at compiler level.

Note 2:

I m quite sure i m making something wrong, because when using STL containers i do not need to instanciate it whenever i use it with a self defined type like :

struct myStruct{
    int a;
    std::string;
};
std::vector<myStruct> a;

Thanks in advance to any people sharing interest in this question.

EDIT :

Something must be utterly wrong in my implementation, as i move my header file into my main and stubbed implementation using {} at end of declaration. and everything compiles fine without the template class T3B_data; declaration.

Question ending up being a duplicate to separating constructor implementation with template from header file

Community
  • 1
  • 1
VivienLeger
  • 577
  • 4
  • 6
  • The reason for your confusion is not quite clear. Your tempate class can be used absolutely the same way as templates from STL. – user3159253 Apr 02 '16 at 19:21
  • e.g. you can create variables `T3B_data t3b;` as you did with `std::vector r;` – user3159253 Apr 02 '16 at 19:26
  • and vice versa: in certain situations explicit template parameter specialization is required whether it's an STL template or user-defined. New allocation is one of those – user3159253 Apr 02 '16 at 19:32
  • thanks for you answer , still my g++ refused to find constructor for T3B_data::T3B_data() until i added the last line in the header.. so yeah, i m still confused as removing the line outputs :`/auxDrives/Work/T3Bn/./main.cpp:7: undefined reference to `T3B_data::T3B_data()'` – VivienLeger Apr 02 '16 at 19:48

1 Answers1

0

If you are using an implementation file and header file for a template class then the compiler needs to know any types that will be used with the class for memory reservation reasons therefore you have to explicitly declare template class T3B_data<int> (as you have done) if you use an int type as a template argument for the class somewhere else in the program.

To avoid having to do this you can define and implement all methods of the template class in the header file instead of using a separate implementation file (preferable depending upon your requirements and the size of the template class, in terms of lines of code).

sjrowlinson
  • 3,297
  • 1
  • 18
  • 35