模板類 宣告與定義

2022-06-02 12:57:11 字數 372 閱讀 1305

模板類的宣告與定義

最近在編寫模板類時發現乙個問題:

當把模板類分開為  .h宣告檔案 和  .cpp實現檔案時,在模板類的main檔案使用模板功能時,編譯結果完全正確,卻無法鏈結成功。

但是,如果把main放到.cpp檔案中就沒有任何問題,或者把 .cpp 和 .h 檔案放在一起也沒有問題。

於是摸索著學習,解決問題有兩種方式:

1. 在呼叫模板類時引入其.cpp實現檔案

2. 將模板類的.cpp實現檔案全部寫進.h中

為什麼?

請參見文章《為什麼c++編譯器不能支援對模板的分離式編譯》

good  good  study, day  day  up.

順序  選擇  迴圈  總結

C 模板類定義與宣告

pragma once ifndef grid h define grid h template class grid int getwidth const static const int kdefaultwidth 10 static const int kdefaultheight 10 pr...

模板類的宣告與定義

newhandlersupport.h 檔案 template class newhandlersupport template new handler newhandlersupport currenthandler template newhandlersupport newhandlersup...

C 模板類定義與宣告

宣告和使用類模板 如果在類模板外定義成員函式,應寫成類模板形式 template 測試的模板類,如下 ifndef compare h define compare h template class compare endif include compare.h template compare c...