簡體   English   中英

g ++和模板的鏈接器錯誤

[英]Linker error with g++ and templates

我正在做一個簡單的程序,當我找不到編譯它的方法時,我正在使用C ++中的模板來實現帶有一些數據類型的庫。

代碼是:

VectorT.cpp文件:

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

VectorT.h文件:

#ifndef __VectorT_h__
#define __VectorT_h__

template <class T>
class VectorT{

  private:

    T * elementos;
    int numElementos;

  public:

    VectorT();
};

#include "VectorT.tpp"
#endif

以及編譯方式,並帶有錯誤:

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

任何想法? 非常感謝。

該“代碼”無法編譯,因為它仍然不是完整的代碼。

這樣,您可以編寫測試代碼,只需要添加頭文件。 例如:g ++ -o bin / testVector src / testVector.cpp -I include /該程序無需編譯即可運行! 唯一的細節是頭文件和cpp文件必須位於/ include文件夾“ VectorT.tpp”和VectorT.h中,以及頭文件和模板的實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM