[問題] linker error?

看板C_and_CPP作者 (神奇)時間15年前 (2010/08/15 20:28), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 開發平台: (例: Dev-C++, Windows) 補充說明: /********************************************* .h檔 #ifndef MATRIX_H #define MATRIX_H #include "cmath" template <class T> class Matrix { public: int nRow; int nCol; T **z; Matrix(); }; #endif **********************************************/ /********************************************** .cpp檔 #include <iostream> #include "Matrix.h" template <class T> Matrix<T>::Matrix() { this->nRow = 0; this->nCol = 0; this->z = NULL; } ***********************************************/ int main(int argc, char *argv[]) { Matrix <int> x; system("PAUSE"); return EXIT_SUCCESS; } dev 出現 [Linker error] undefined reference to `Matrix<int>::Matrix()' 但是如果把.cpp的code寫在.h裡面 就可以正常編譯 請問這是什麼問題? 第一次用template 還請大家指點一下 謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.231.212

08/15 20:36, , 1F
#1C1pLSrL的推文
08/15 20:36, 1F

08/15 20:45, , 2F
其中要注意到 C++0x已將export列為depercate了
08/15 20:45, 2F

08/15 20:46, , 3F
不過也沒差 因為真正實作出來的就我所知也就那麼一家...
08/15 20:46, 3F

08/15 20:51, , 4F
現在template主要還是inclusion model吧?
08/15 20:51, 4F

08/15 21:07, , 5F
以置入性模型來說, 要具現化建構子出來, 前提是你在具
08/15 21:07, 5F

08/15 21:09, , 6F
現的點要看得到建構子的「定義」, 分不同檔案看不到,
08/15 21:09, 6F

08/15 21:09, , 7F
就這麼簡單
08/15 21:09, 7F
文章代碼(AID): #1CPzrnHv (C_and_CPP)