[問題] VS2008的linking error

看板C_and_CPP作者 (Soccer Fever)時間14年前 (2009/11/21 13:43), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串1/1
Matrix.h定義以下函式 template <class ElType> matrix<ElType> operator*(const ElType& x, const matrix<ElType>& s) { ... } template <class ElType> class matrix { ... friend matrix<ElType> operator*(const ElType&, const matrix<ElType>&); }; 然後在別的檔案使用matrix函數 matrix<double> m1, m2; m2 = -1 * m1; VS2008編譯的時候會出現linking error LNK2001: unresolved external symbol "class matrix<double> __cdecl operator*(double const &,class matrix<double> const &)" (??D@YA?AV?$matrix@N@@ABNABV0@@Z) 我找了很久實在找不出哪裡有錯 這裡的泛型有用錯嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 128.61.49.144

11/21 13:47, , 1F
friend 的 template parameter 要另外定義
11/21 13:47, 1F

11/21 14:39, , 2F
template 的 friend operator要直接定義在class裡
11/21 14:39, 2F

11/21 14:47, , 3F
成功了 原來是我語法不熟 orz 感謝樓上兩位
11/21 14:47, 3F
文章代碼(AID): #1B1ttdZk (C_and_CPP)