[問題] C++ Template

看板Programming作者 (oi)時間14年前 (2011/01/01 01:56), 編輯推噓3(304)
留言7則, 5人參與, 最新討論串1/1
請教一個問題 我有如下一段程式碼 要讓函式接收各種型態的Vector 但無法編譯 請問要如何修改 謝謝 #include <iostream> #include <vector> template <typename T> int vecSize(vector<T> a); using namespace std; int main() { vector<int> vec(10,0); cout << vecSize(vec) << endl; return 0; } template <typename T> int vecSize(vector<T> a) { return a.size(); } 錯誤訊息如下 D:\My Dropbox\School\Digital Image Processing\fftDev\main.cpp|3|error: template declaration of 'int vecSize'| D:\My Dropbox\School\Digital Image Processing\fftDev\main.cpp|3|error: 'vector' was not declared in this scope| D:\My Dropbox\School\Digital Image Processing\fftDev\main.cpp|3|error: expected primary-expression before '>' token| D:\My Dropbox\School\Digital Image Processing\fftDev\main.cpp|3|error: 'a' was not declared in this scope| D:\My Dropbox\School\Digital Image Processing\fftDev\main.cpp||In function 'int main()':| D:\My Dropbox\School\Digital Image Processing\fftDev\main.cpp|10|error: 'vecSize' was not declared in this scope| ||=== Build finished: 5 errors, 0 warnings ===| -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.232.184.24

01/01 02:04, , 1F
你的你prototype放在using前面,他認不得vec
01/01 02:04, 1F

01/01 02:09, , 2F
原來只有一行之差,感謝您!!
01/01 02:09, 2F

01/01 21:49, , 3F
這是線上編譯嗎??
01/01 21:49, 3F

01/01 21:56, , 4F
樓上的意思是? >"<
01/01 21:56, 4F

01/03 19:25, , 5F
就是compiler在dropbox上嗎?
01/03 19:25, 5F

01/03 19:25, , 6F
不然error msg怎麼印出來在那邊XD
01/03 19:25, 6F

01/05 01:46, , 7F
那個是本機資料夾而已吧 XD
01/05 01:46, 7F
文章代碼(AID): #1D7XbBhZ (Programming)
文章代碼(AID): #1D7XbBhZ (Programming)