自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++模板坑,一起來issue

開發(fā) 后端
C++開發(fā)中通常將類定義放在C ++頭文件(.h)中,并將實現(xiàn)放在C ++源文件(.cpp)中。然后,將源文件作為項目的一部分,這意味著將其單獨編譯。但是,當(dāng)我們對模板類實施此過程時,將出現(xiàn)一些編譯和鏈接問題。本文闡述了三種可能的解決方案,幫助大家可以在實現(xiàn)該模板的源文件中創(chuàng)建一個模板類的對象,解決上述問題。

[[329857]]

C++開發(fā)中通常將類定義放在C ++頭文件(.h)中,并將實現(xiàn)放在C ++源文件(.cpp)中。然后,將源文件作為項目的一部分,這意味著將其單獨編譯。但是,當(dāng)我們對模板類實施此過程時,將出現(xiàn)一些編譯和鏈接問題。

本文闡述了三種可能的解決方案,幫助大家可以在實現(xiàn)該模板的源文件中創(chuàng)建一個模板類的對象,解決上述問題。

問題復(fù)現(xiàn)

頭文件聲明:

 

  1. // temp.h 
  2. #ifndef _TEMP_H_ 
  3. #define _TEMP_H_ 
  4. #include <iostream> 
  5. #include <vector> 
  6. template <typename T> 
  7. using Vec = std::vector<T>; 
  8. #define PRINTFMT(x) std::cout << x << " "
  9.  
  10. template <typename T> 
  11. void TestTemp(const Vec<T> &v, T target); 
  12. #endif 

頭文件實現(xiàn):

 

  1. #include "temp.h" 
  2.  
  3.  
  4. template <typename T> 
  5. void TestTemp(const Vec<T> &v, T target) 
  6.     [=]() { 
  7.         for (auto elem : v) 
  8.             if (elem == target) 
  9.                 PRINTFMT(elem); 
  10.     }(); 

報錯:

 

  1. undefined reference to.... 

問題描述:當(dāng)在.h中聲明了模板,.cpp中定義了模板,當(dāng)main函數(shù)去進行模板實例化的時候,在聲明處找不到對應(yīng)的T類型,自然就出問題了。

1.第一種:同一文件

聲明及定義都在.h文件中。

 

  1. // temp.h 
  2. #ifndef _TEMP_H_ 
  3. #define _TEMP_H_ 
  4. #include <iostream> 
  5. #include <vector> 
  6. template <typename T> 
  7. using Vec = std::vector<T>; 
  8. #define PRINTFMT(x) std::cout << x << " "
  9.  
  10. template <typename T> 
  11. void TestTemp(const Vec<T> &v, T target) 
  12.     [=]() { 
  13.         for (auto elem : v) 
  14.             if (elem == target) 
  15.                 PRINTFMT(elem); 
  16.     }(); 
  17. #endif 

2.第二種:分離開+引入頭文件

采用頭文件聲明,cpp定義,要想起作用,得在使用處引入兩者并且定義處得用特化版本。

例如:

頭文件實現(xiàn):

 

  1. // Temp.cpp 
  2. #include "temp.h" 
  3.  
  4. void TestTemp(const Vec<int> &v, int target) 
  5.     [=]() { 
  6.         for (auto elem : v) 
  7.             if (elem == target) 
  8.                 PRINTFMT(elem); 
  9.     }(); 
  10.  
  11. template <typename T> 
  12. void TestTemp(const Vec<T> &v, T target) 
  13.     [=]() { 
  14.         for (auto elem : v) 
  15.             if (elem == target) 
  16.                 PRINTFMT(elem); 
  17.     }(); 

實現(xiàn):

 

  1. #include "temp.h" 
  2. #include "temp.cpp" 
  3.  
  4. int main() { 
  5.     std::vector<int> v{1,2,3}; 
  6.     int target = 2; 
  7.     TestTemp<int>(v,target); 
  8.  
  9.     return 0; 

3.在末尾引入cpp

只需要在.h頭文件末尾引入cpp即可。

頭文件只需要聲明:

 

  1. // temp.h 
  2. #ifndef _TEMP_H_ 
  3. #define _TEMP_H_ 
  4. #include <iostream> 
  5. #include <vector> 
  6. template <typename T> 
  7. using Vec = std::vector<T>; 
  8. #define PRINTFMT(x) std::cout << x << " "
  9.  
  10. template <typename T> 
  11. void TestTemp(const Vec<T> &v, T target); 
  12. #include "temp.cpp" 
  13. #endif 

頭文件定義即可:

 

  1. // Temp.cpp 
  2. #include "temp.h" 
  3.  
  4. template <typename T> 
  5. void TestTemp(const Vec<T> &v, T target) 
  6.     [=]() { 
  7.         for (auto elem : v) 
  8.             if (elem == target) 
  9.                 PRINTFMT(elem); 
  10.     }(); 

調(diào)用處正常調(diào)用:

 

  1. #include "temp.h" 
  2.  
  3. int main() { 
  4.     std::vector<int> v{1,2,3}; 
  5.     int target = 2; 
  6.     TestTemp<int>(v,target); 
  7.  
  8.     return 0; 

在一些開源項目中,這種方式比較常見,只不過這里的.cpp得改為.hpp。其余不變!

4.總結(jié)

本節(jié)針對日常代碼中的難點進行了梳理,提出了幾種解決方案??梢院唵蔚陌涯0謇斫鉃橐环N特殊的宏,模板類不要當(dāng)作類,在被實例化的時候一定得找到定義,不然只看到聲明,就GG了。

責(zé)任編輯:龐桂玉 來源: C語言與C++編程
相關(guān)推薦

2012-04-14 20:47:45

Android

2012-06-25 09:37:24

Web

2012-11-08 17:33:53

智慧云

2021-06-09 08:15:50

volatileJava開發(fā)

2009-10-29 16:32:34

Oracle表空間

2010-05-21 17:32:07

IIS服務(wù)器

2012-09-10 13:42:55

PHP項目管理

2021-04-26 11:18:15

FedoraLinuxBug

2021-06-02 15:30:12

Synchronize并發(fā)多線程

2011-09-07 22:59:07

聯(lián)想一體機

2022-05-20 12:14:50

ZuulSpringClou

2010-05-10 15:31:35

Unix文件

2009-07-14 16:35:57

Swing組件大全

2012-07-10 09:14:51

Web

2017-11-02 15:28:52

2009-09-09 09:23:37

服務(wù)器穩(wěn)定性

2017-11-02 14:39:54

2021-07-28 14:15:51

漏洞懸賞平臺漏洞谷歌

2021-01-20 15:31:00

區(qū)塊鏈比特幣數(shù)字貨幣
點贊
收藏

51CTO技術(shù)棧公眾號