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.