0

I have a simple C++ class as follows:

class  __declspec(dllexport) PrefData
{
public:
  PrefData();
  int m_data_member;
};

std::ostream& operator<<(std::ostream& os, const PrefData& obj);

This results in an unresolved external error with unresolved external symbol class std::basic_ostream > & __cdecl cop4530::operator<<. Now, I tried to make it a class member as:

class  __declspec(dllexport) PrefData
{
public:
  PrefData();
  int m_data_member;
  friend std::ostream& operator<<(std::ostream& os, const PrefData& obj);
};

This also resulted in the same error. However, when I exported it as:

class  __declspec(dllexport) PrefData
{
public:
  PrefData();
  int m_data_member;
};

__declspec(dllexport) std::ostream& operator<<(std::ostream& os, const PrefData& obj);

This links fine. I am not sure why it did not link when it was a class member function as the dllexport was applied to the whole class? Secondly, is this a bad idea to export this operator? I read somewhere that it may not be a good thing to do but I could not figure out the details.

Luca
  • 10,458
  • 24
  • 107
  • 234
  • 2
    Just for your clarification, the second alternative doesn't make the function a member-function. It is still a non-member function, but ***declared*** in the scope of the `PrefData` class. The ***definition*** is still not (I assume) marked as an exported function. It would probably have worked if you declared the function as exported inside the class. – Some programmer dude Mar 16 '17 at 06:43
  • Thank you for that. I did not know that. Is it all right to declare the function as exported? I am not sure if it is good to do that for operators across DLL boundaries. At least I read something to that effect. – Luca Mar 16 '17 at 07:23

0 Answers0