简体   繁体   中英

Linker error with g++ and templates

I'm doing a simple program and I'm using templates in C++ to implement a library with a few data types when I don't find the way to compile it.

The code is:

VectorT.cpp file:

template <class T>
VectorT<T>::VectorT(){
  elementos = NULL;
  numElementos=0;
}

VectorT.h file :

#ifndef __VectorT_h__
#define __VectorT_h__

template <class T>
class VectorT{

  private:

    T * elementos;
    int numElementos;

  public:

    VectorT();
};

#include "VectorT.tpp"
#endif

And the compile way, with the error:

g++ -c -o obj/VectorT.o src/VectorT.cpp -I include/VectorT.h 
g++: warning: src/VectorT.cpp: linker input file unused because linking not done

Any idea? Thank you so much.

This "code" not can be compiled, because it still aren't a complete code.

This way you can write a test code, and you only will need add the headers files. For example: g++ -o bin/testVector src/testVector.cpp -I include/ And the program works without compiling anything else! The only detail is that headers files and cpp files must be in /include folder, "VectorT.tpp" and VectorT.h, the headers and the implementation of templates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM