作者prodigywu (Soccer Fever)
看板C_and_CPP
標題[問題] VS2008的linking error
時間Sat Nov 21 13:43:00 2009
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
1F:推 legnaleurc:friend 的 template parameter 要另外定義 11/21 13:47
2F:→ ADF:template 的 friend operator要直接定義在class裡 11/21 14:39
3F:→ prodigywu:成功了 原來是我語法不熟 orz 感謝樓上兩位 11/21 14:47