我在(VC++ 2013)DLL中定义了<<
运算符重载,可以很好地进行编译:
定义:
__declspec(dllexport) friend std::ostream& operator<< (std::ostream& os, const ComplexMessage& rhs);
实现方式:
std::ostream& operator<< (std::ostream& os, const ComplexMessage& rhs)
{
os << rhs.toString();
return(os);
}
该dll包含另外50种方法,其中包括多个运算符重载,可以进行编译和链接。
但是,使用dll的程序无法链接<
它声明
<<
重载为__declspec(dllimport) std::ostream& operator<< (std::ostream& os, const ComplexMessage& rhs);
该代码可以正常编译。但它不会链接:
所有其他DLL方法链接正常。有谁知道为什么发生此链接器错误?
编辑
这与建议的重复问题不同。这些符号在DLL的代码中定义,并通过语法进行编译;但是,它没有链接。这向我表明,该特定运算符<
最佳答案
由于上面的输入,确定解决方案是在实现中显式指定 namespace (而不是定义):
std::ostream& messaging::operator<< (std::ostream& os, const ComplexMessage& rhs);
注意,使用“使用 namespace 消息传递”子句无效。链接器要求在实现声明中明确指定它。