0

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.

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
  • 2
    Why are you compiling a header file? You're supposed to `#include` that file in one or more of your source files, not compile it separately. – PaulMcKenzie Nov 10 '15 at 21:41
  • **One** Don't use .tpp file suffix, it's not standard and g++ doesn't understand what kind of file it is. C++ source should go a C++ source file. Those have a known list of suffixes: .cc, .cpp, .cxx, .c++, .C. You can override this but it's not recommended. – n. m. could be an AI Nov 10 '15 at 21:48
  • 1
    **Two** see [this](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – n. m. could be an AI Nov 10 '15 at 21:49
  • I want compile this .o file to add to a library after. I have been doing this many times (the compilation way) without problems until I have been needing templates. How would you do it? – Juan Antonio Nov 10 '15 at 21:49
  • 2
    You cannot do this with templates. Templates go to header files. See the link in a comment above. – n. m. could be an AI Nov 10 '15 at 21:50
  • How said Luc Touraille "This way, implementation is still separated from declaration, but is accessible to the compiler." So, I think that the problem is how I have used g++. What do you think? How would be the way? – Juan Antonio Nov 10 '15 at 22:02
  • 1
    @JuanAntonio Templates are not "real" code -- that's why they're called *templates*. The real code happens when you *instantiate* the template with the concrete types, for example `std::vector`. – PaulMcKenzie Nov 10 '15 at 22:09
  • Oh, you are right. This "code" not can be compiled, because it still aren't a complete code. I understand this, so, And if I want do a library with many objects with templates, I can't do it? For share with a friends maybe... – Juan Antonio Nov 10 '15 at 22:30

1 Answers1

0

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.

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34