[問題] template實作問題
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
遇到的問題: (題意請描述清楚)
看C++ primer 4/e中寫說,要在編譯期讓compiler看到template class的定義才能實體化
而書中寫出兩種編譯模式 (p. 644)
我嘗試使用第一種置入式的,結果一直說我重複定義...
另外,第二種分離式的好像沒支援?
希望得到的正確結果:
編譯成功,可以跑
程式跑出來的錯誤結果:
我在linux上,用 g++ -o template.cpp ta.cpp
ta.cpp:4: error: redefinition of ‘const int A<T, N>::a’
ta.h:6: error: ‘const int a’ previously declared here
ta.cpp:5: error: redefinition of ‘void A<T, N>::print(T (&)[N])’
ta.cpp:5: error: ‘void A<T, N>::print(T (&)[N])’ previously declared here
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
linux, g++
有問題的code: (請善用置底文標色功能)
ta.h
http://codepad.org/nFtPiBOD
ta.cpp
http://codepad.org/p0R54fe6
template.cpp
http://codepad.org/epIdYZ7A
補充說明:
後來我將ta.h中的 #include "ta.cpp" 移除掉
並將template.cpp中 #include "ta.h" 改成 #include "ta.cpp" 卻可以正常執行了
但是跟書上不太一樣 = =
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.208.235
→
11/09 21:45, , 1F
11/09 21:45, 1F
→
11/09 21:46, , 2F
11/09 21:46, 2F
→
11/09 21:46, , 3F
11/09 21:46, 3F
→
11/09 21:48, , 4F
11/09 21:48, 4F
→
11/09 21:48, , 5F
11/09 21:48, 5F
→
11/09 22:00, , 6F
11/09 22:00, 6F
→
11/09 22:06, , 7F
11/09 22:06, 7F
→
11/09 22:07, , 8F
11/09 22:07, 8F
→
11/09 22:08, , 9F
11/09 22:08, 9F
→
11/09 22:09, , 10F
11/09 22:09, 10F
→
11/09 22:09, , 11F
11/09 22:09, 11F
→
11/09 22:11, , 12F
11/09 22:11, 12F