我正在创建这个类:
///> MyObject.h file
class __declspec(dllexport) CMyObject
{
public:
int m_Intero;
public:
CMyObject();
~CMyObject();
public:
friend std::wifstream& operator>>(std::wifstream& is, CMyObject& eprt);
}
///> MyObject.cpp
std::wifstream& operator>>(std::wifstream& is, CMyObject& myobj)
{
if (is.is_open())
///> Input operations.
return is;
}
当我编译这个库时我没有得到任何错误,但是当我在最终项目中使用我的库时我得到这个错误:
LNK2019 unresolved external symbol reference "class std::basic_ifstream<wchar_t,struct std::char_traits<wchar_t> > & __cdecl operator>>(...) in function "public: void __thiscall ...
我想我必须以某种方式指定必须导出我的 operator>> 函数。
我应该如何修改我的代码?
最佳答案
几个方面:
- 您导出的是类,而不是全局(友元)函数。您只是让编译器知道全局函数(运算符重载)是一个 friend 。您需要导出函数。
- 当涉及到 C++ 对象时,最好不要导出此类函数,因为编译器的差异会使类具有不同的大小。
关于c++ - dll导出友元运算符>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36426486/