简体   繁体   English

g ++和模板的链接器错误

[英]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. 我正在做一个简单的程序,当我找不到编译它的方法时,我正在使用C ++中的模板来实现带有一些数据类型的库。

The code is: 代码是:

VectorT.cpp file: VectorT.cpp文件:

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

VectorT.h file : VectorT.h文件:

#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! 例如:g ++ -o bin / testVector src / testVector.cpp -I include /该程序无需编译即可运行! 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. 唯一的细节是头文件和cpp文件必须位于/ include文件夹“ VectorT.tpp”和VectorT.h中,以及头文件和模板的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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