Reputation: 4757
I have a C++ class inside a dll.
In that class I want to store data from a Curl callback into a member variable. I was going to use a stringstream like so:
void MyClass::CurlCallback( void * pvData, size_t tSize )
{
const char* data = static_cast<const char*>(pvData);
m_myStringStream << sata;
}
But when declare the stringstream in my class like so:
private:
std::stringstream m_myStringStream;
I get the following error:
Error 1 error C2220: warning treated as error - no 'object' file generated
Warning 2 warning C4251: 'MyClass::MyClass::m_myStringStream' : class 'std::basic_stringstream<_Elem,_Traits,_Alloc>' needs to have dll-interface to be used by clients of class 'MyClass::MyClass'
How can I declare this stringstream without getting this error?
I assume it is because stringstream is a C++ variable but the dll is expecting c style variables.
I have investigated maybe creating a class that stores the xml data like so:
class XMLData
{
public:
XMLData();
virtual ~ XMLData();
const char* GetXMLData() const { return xml; }
void Append( const char* xmlData ) { /*add xmlData to xml blah blah*/};
private:
//Add more here - to do
char* xml;
int length;
};
and declaring it:
XMLData* m_xmlData;
What is the best way to do this??
Upvotes: 2
Views: 1071
Reputation: 8313
First of all, you get a warning, you choose to threat all warnings like errors in your project settings.
A DLL exported class shouldn't declare complex types (like STL templates) in its exported interface, because it limits the DLL usage to the very same version of the compiler. that is why you got the warning.
to solve this you should export only an interface class, (i.e. pure abstract class) and return an implementation of the interface.
like this:
//in the interface:
class DLL_API IMyClass
{
public:
virtual void set(const char* data)=0;
virtual const char* get()=0;
}
//now not in the interface:
class CMyClass : public IMyClass
{
private:
std::stringstream mystream;
public:
virtual void set(const char* data){
mystream<<data;
}
virtual const char* get(){
return mystream.str().c_str();
}
}
And you using only references or pointers outside the DLL, if you need to create object in the executable you'll need a factory method in the DLL, since it only knows the interface.
IMyClass* ObjectFactory::GetMyClass()
{
return new CMyClass();
}
Upvotes: 2