[問題] template的問題

看板C_and_CPP (C/C++)作者 (heymei)時間14年前 (2012/03/22 00:43), 編輯推噓4(4010)
留言14則, 5人參與, 最新討論串1/1
大家好 小弟目前在用code::block在寫相關的template的題目 我餵進去的資料類型有 unsigned,signed,char 簡單來說,我寫的程式就是將上述類型的資料輸入,然後做排序 程式碼我擷取重點 int main() { unsigned a[11]={UINT_MAX,777,INT_MAX,7,999999999,INT_MAX+1u,....} //略 binary_qsort(a,11); // 利用這個函式做排序 } template<typename T> void binary_qsort(T *t,int n) { bool arr[n][32] // n是傳進來的排序數目,32是指位元 .....略 Qsort(arr,0,n) // compiler在這把我擋下 } void Qsort(bool A[][32],int l,int h) { //略 主要做QuickSort } compiler把我擋下原因是: there are no arguments to `Qsort' that depend on a template parameter so a declaration of `Qsort' must be available 樣板裡面的函式 一定也要是樣板嗎? 我覺得compiler擋得滿沒道理的 所以特來問問 QQ 若想看比較完整的 --> http://codepad.org/JERFxMYg [但沒寫完] -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.37.168.153

03/22 00:48, , 1F
如果你把Qsort宣告在binary_qsort之前呢?
03/22 00:48, 1F

03/22 00:52, , 2F
沒這問題了= =
03/22 00:52, 2F

03/22 00:53, , 3F
哈哈 sry XDDD
03/22 00:53, 3F

03/22 01:11, , 4F
請 google forward declaration
03/22 01:11, 4F

03/22 01:51, , 5F
不過compiler 的message是什麼道理?
03/22 01:51, 5F

03/22 01:51, , 6F
這error跟template無關吧
03/22 01:51, 6F

03/22 05:57, , 7F
我猜啦 通常我們在寫 template 函式的時候
03/22 05:57, 7F

03/22 05:57, , 8F
當中呼叫的函式很有可能和 template 相關
03/22 05:57, 8F

03/22 05:58, , 9F
啊等等我漏看了一件事 上兩行請忽略
03/22 05:58, 9F

03/22 05:59, , 10F
應該是這樣: 因為 no arguments depend on template param.
03/22 05:59, 10F

03/22 05:59, , 11F
這個 Qsort 的呼叫的型態裡面沒有 T
03/22 05:59, 11F

03/22 06:00, , 12F
所以 compiler 告訴你這是個普通函式呼叫 請先宣告
03/22 06:00, 12F

03/22 06:10, , 13F
剛剛咕狗到 gcc 對此的說明: http://tinyurl.com/3tqroqf
03/22 06:10, 13F

03/22 06:10, , 14F
這應該也能解釋為何這種情形要特別使用錯誤訊息告知
03/22 06:10, 14F
文章代碼(AID): #1FQWKRSU (C_and_CPP)
文章代碼(AID): #1FQWKRSU (C_and_CPP)