我設計一個函式使用函式範本(Function template)
//--------------------------------
在tools_hou.h
template <typename T1, typename T2> //class or typename皆可
T1 add(T1 x, T2 y);
//--------------------------------
在tools_hou.cpp
template <typename T1, typename T2> //class or typename皆可
T1 add(T1 x, T2 y)
{
return x + y;
}
//-------------------------------
在主程式Unit8.cpp
#include "tools_hou.h"
__fastcall TForm8::TForm8(TComponent* Owner)
: TForm(Owner)
{
Label1->Caption = add(1,100);
Label2->Caption = add(1.23,100.34);
}
//-------------------------------
結果編譯錯誤,訊息如下:
Build
[Linker Error] Unresolved external 'int add<int, int>(int, int)' referenced from D:\BCB6PROJECTS\UNIT1.OBJ
[Linker Error] Unresolved external 'double add<double, double>(double, double)' referenced from D:\BCB6PROJECTS\UNIT1.OBJ
請問如何解決?
(如果我將add函數寫在主程式內,則無問題)