看板 EE_DSnP 關於我們 聯絡資訊
(這是一個使用template class,在呼叫時compile不過的問題) 作業上說selectionSort的prototype是這樣 template <class T> void selectionSort(vector<T>& array, const Compare<T>& compare) 我在main()裡面要呼叫的時候,寫 selectionSort( <int>data, Greater <int> () ); <----compile不過 ^^^^data是我用來存字串array的vector 我的Greater是這樣定義的 template <class T> class Greater: public Compare<T> { ... } 結果compile不過,訊息是 ( 錯在main()中呼叫selectionSort()的那一行 ) error: expected primary-expression before '<' token error: expected primary-expression before 'int' 請問:是我呼叫"functional object兼template"的方法錯誤嗎? 我只知道函數的template呼叫時是: f<int>(a, b); 而class的template要instantiate一個物件的時候是用: myClass <int> (); 但是同時要用functional object(沒有object)配上template,該怎麼做呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.218.186 ※ 編輯: wintercobra 來自: 140.112.218.186 (10/15 16:45)
tomap41017:你selectionSort的第一個參數不覺的怪怪的嗎@@? 10/15 20:58
tomap41017:應該是selectionSort(data, Greater<int>()); 10/15 20:58
tomap41017:template的宣告應該是ok... 10/15 20:59
tomap41017:你的編譯器告訴你的錯誤訊息就是指那個地方有錯囉 10/15 21:00
Astone:問題+1 complie在呼叫函式時一直過不了= = 10/15 22:16
cktigeryang:他會自己配對,你就把它想成是overload一個函式就好了 10/15 22:31
ric2k1:推 selectionSort(data, Greater<int>()); 10/15 23:02
wintercobra:原來他自己會判定data啊...謝謝~ 10/15 23:35
aitjcize:Automatic type dedutction http://goo.gl/OPVu 10/16 14:37
tomap41017:樓上的感謝你XD 10/17 23:12