链接器工具错误 LNK2019 必须在友元声明中显式指定模板参数
摘自MSDN:ms-help://MS.MSDNQTR.v90.chs/dv_vccomp/html/4392be92-195c-4eb2-bd4d-49cfac3ca291.htm
下面的示例生成与用户定义的运算符有关的 LNK2019。
// LNK2019e.cpp // compile with: /EHsc // LNK2019 expected #include <iostream> using namespace std; template<class T> class Test { friend ostream& operator<<(ostream&, Test&); // Uncomment the following line to resolve. // template<typename T> friend ostream& operator << (ostream&, Test<T>&); }; template<typename T> ostream& operator<<(ostream& os, Test<T>& tt) { return os; } int main() { Test<int> t; cout << "Test: " << t << endl; // unresolved external }
为 Visual Studio .NET 2003 进行的一致性工作(模板友元和专用化)也可能导致 LNK2019。在 Visual Studio .NET 2003 中,除非在友元声明中显式指定了模板参数,否则与函数模板同名的友元函数的声明不会引用该函数模板。
如果您未指定模板参数,则友元声明将声明一个非模板函数(可能是int)。
有关更多信息,请参见
要使代码在 Visual C++ 的 Visual Studio .NET 2003 和 Visual Studio .NET 版本中均有效,请显式指定友元函数的模板参数列表。