Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dll export friend operator>>

I'm creating this class:

///> 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;
}

When I compile the library I get no errors, but when I use my library in the final project I get this error:

LNK2019 unresolved external symbol reference "class std::basic_ifstream<wchar_t,struct std::char_traits<wchar_t> > & __cdecl operator>>(...) in function "public: void __thiscall ...

I guess I have to specify somehow that my operator>> function have to be exported.

How should I modify my code?

like image 874
IssamTP Avatar asked Oct 28 '25 03:10

IssamTP


1 Answers

Few aspects:

  • You are exporting the class, not the global (friend) function. You're just letting the compiler know that global function (operator overload) is a friend. You need to export the function.
  • When C++ objects are concerned, you better not export such functions, because compiler differences will make classes to have different sizes.
like image 190
Ajay Avatar answered Oct 29 '25 17:10

Ajay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!