線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1130
推到 Plurk!
推到 Facebook!

template 無法 link

尚未結案
dadai
一般會員


發表:29
回覆:31
積分:12
註冊:2009-01-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-10-28 10:55:12 IP:219.90.xxx.xxx 未訂閱
小弟第一次使用template
遇到了一些奇怪的問題
想請前輩們指導一下 @@

以下是我的template的寫法
TestTemplate.h
[code cpp]
class TestTemplate
{
public:
static template T __fastcall ReturnValue(const T Value);
};
[/code]

TestTemplate.cpp
[code cpp]
template T __fastcall TestTemplate::ReturnValue(const T Value)
{
return Value;
}
[/code]

以下是測試用的程式碼
[code cpp]
void __fastcall TTestForm::Button1Click(TObject *Sender)
{
ShowMessage( IntToStr(TestTemplate::ReturnValue(100)) );
ShowMessage( FloatToStr(TestTemplate::ReturnValue(3.1416)) );
ShowMessage( TestTemplate::ReturnValue(L"Test") );
}
[/code]

build沒問題
但是 link 出現下列錯誤訊息
[ILINK32 Error] Error: Unresolved external 'int __fastcall TestTemplate::ReturnValue(const int)' referenced from C:\MYTESTENVIRONMENT\RELEASE\FRM_TESTDRAGMAIN.OBJ
[ILINK32 Error] Error: Unresolved external 'double __fastcall TestTemplate::ReturnValue(const double)' referenced from C:\MYTESTENVIRONMENT\RELEASE\FRM_TESTDRAGMAIN.OBJ
[ILINK32 Error] Error: Unresolved external 'const wchar_t * __fastcall TestTemplate::ReturnValue(const wchar_t * const)' referenced from C:\MYTESTENVIRONMENT\RELEASE\FRM_TESTDRAGMAIN.OBJ

請問是我有忽略什麼東西嗎 @@
或是我的觀念不正確?
編輯記錄
dadai 重新編輯於 2010-10-27 20:56:21, 註解 無‧
dadai 重新編輯於 2010-10-27 20:57:12, 註解 無‧
dadai 重新編輯於 2010-10-27 21:17:27, 註解 無‧
dadai
一般會員


發表:29
回覆:31
積分:12
註冊:2009-01-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-10-29 11:00:55 IP:219.90.xxx.xxx 未訂閱
自問自答

改用下列方式就可以了

TestTemplate.h
[code cpp]
class TestTemplate
{
public:
template
static T __fastcall ReturnValue(const T value)
{
return value;
}
};
[/code]

看來小弟對於 C 的 template 以及一些基本概念還不甚清楚
還有很多要加強的地方...Orz

小弟告退

===================引 用 dadai 文 章===================
小弟第一次使用template
遇到了一些奇怪的問題
想請前輩們指導一下 @@

以下是我的template的寫法
TestTemplate.h
[code cpp]
class TestTemplate
{
public:
static template T __fastcall ReturnValue(const T Value);
};
[/code]

TestTemplate.cpp
[code cpp]
template T __fastcall TestTemplate::ReturnValue(const T Value)
{
return Value;
}
[/code]

以下是測試用的程式碼
[code cpp]
void __fastcall TTestForm::Button1Click(TObject *Sender)
{
ShowMessage( IntToStr(TestTemplate::ReturnValue(100)) );
ShowMessage( FloatToStr(TestTemplate::ReturnValue(3.1416)) );
ShowMessage( TestTemplate::ReturnValue(L"Test") );
}
[/code]

build沒問題
但是 link 出現下列錯誤訊息
[ILINK32 Error] Error: Unresolved external 'int __fastcall TestTemplate::ReturnValue(const int)' referenced from C:\MYTESTENVIRONMENT\RELEASE\FRM_TESTDRAGMAIN.OBJ
[ILINK32 Error] Error: Unresolved external 'double __fastcall TestTemplate::ReturnValue(const double)' referenced from C:\MYTESTENVIRONMENT\RELEASE\FRM_TESTDRAGMAIN.OBJ
[ILINK32 Error] Error: Unresolved external 'const wchar_t * __fastcall TestTemplate::ReturnValue(const wchar_t * const)' referenced from C:\MYTESTENVIRONMENT\RELEASE\FRM_TESTDRAGMAIN.OBJ

請問是我有忽略什麼東西嗎 @@
或是我的觀念不正確?
系統時間:2024-04-24 7:48:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!