[問題] 請問使用template的error

看板C_and_CPP (C/C++)作者 ( 123)時間16年前 (2010/02/11 13:15), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
新手第一次使用function template 程式碼如下 template <class T> T** G(int nRows, int nCols) { T **ppi; T *pool; T *curPtr; ppi = new T*[nRows]; pool = new T [nRows * nCols]; curPtr = pool; for( int i = 0; i < nRows; i++) { *(ppi + i) = curPtr; curPtr += nCols; } return ppi; } main呼叫時 int main(int argc, char *argv[]) { double **d = G(5,3); system("PAUSE"); return EXIT_SUCCESS; } 即出現錯誤 no matching function for call to `G(int, int)' 第一次使用 function template,請問這該怎麼解決呢 希望大家不吝指導 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.103.207

02/11 13:49, , 1F
function template 只拿回傳 type 當參數時,
02/11 13:49, 1F

02/11 13:50, , 2F
要明確指定它的 type
02/11 13:50, 2F

02/11 13:50, , 3F
你的例子中改用 G<double>(5, 3) 即可
02/11 13:50, 3F

02/11 14:00, , 4F
謝謝你 ^____^
02/11 14:00, 4F
文章代碼(AID): #1BSvA3_S (C_and_CPP)
文章代碼(AID): #1BSvA3_S (C_and_CPP)