Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Dll导出友元运算符>&燃气轮机;_C++_Visual C++_Visual Studio 2015 - Fatal编程技术网

C++ Dll导出友元运算符>&燃气轮机;

C++ Dll导出友元运算符>&燃气轮机;,c++,visual-c++,visual-studio-2015,C++,Visual C++,Visual Studio 2015,我正在创建这个类: ///> 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.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 ...
LNK2019未解析外部符号引用“函数中的class std::basic_ifstream&u cdecl运算符>>(…)”public:void_uthiscall。。。
我想我必须以某种方式指定必须导出运算符>>函数

如何修改代码?

几个方面:

  • 您正在导出类,而不是全局(友元)函数。您只是让编译器知道全局函数(运算符重载)是一个好朋友。您需要导出该函数
  • 当C++对象相关时,最好不要导出这些函数,因为编译器差异会使类具有不同的大小。

从第二点开始,我应该假设我不必导出operator>>friend,然后我就不能使用它从文件中加载数据。编辑:我做了一个“LoadFromFile”函数,并在这里使用了操作符>>。是否正确?friend uu declspec(dllexport)std::wifstream&operator>>(std::wifstream&Is,CMyObject&eprt);应该解决你的问题。