I have been writing some code for a lib and experimented with a default Win32 console application to get everything running. Since I have completed all classes I wanted to extract everything into a DLL and therefore I started the adaptation with the usual macro:
#ifdef MYLIB_EXPORTS
#define DllExport __declspec(dllexport)
#else
#define DllExport __declspec(dllimport)
#endif
I am using one interface in my code which is defined like this:
__interface DllExport ISerializable {
void Serialize(/* ... */);
/* some other methods */
};
And this has worked while gaving this code in my exe. In the DLL I get an error during compilation which states
error C2039: '=' : is not a member of 'MyLib::ISerializable'
error C2664: 'MyLib::DerivedClass::operator =' : cannot convert parameter 1 from 'const MyLib::ISerializable' to 'const MyLib::DerivedClass &'
for every class which inherits ISerializable to implement the required methods. (I am using std::shared_ptr<ISerializable> a few times to have abstraction in my code.) However when I change __interface to class and make all methods pure virtual I do not get this error and compilation succeeds.
Why do I get this error? Why does my class/interface in my DLL need the assignment operator? Is there any workaround?
(Using Visual Studio 2012 RTM on Windows 8 RTM with C++11.)
Here is one segment where this error occurs (error point always to the last } of the class):
class DllExport Tile final : public ISerializable {
public:
__declspec(property(get=GetIsPassable, put=SetIsPassable))
bool IsPassable;
__declspec(property(get=GetTileId, put=SetTileId))
uint16_t TileId;
bool& GetIsPassable() { return this->_IsPassable; }
void SetIsPassable(bool val) { this->_IsPassable = val; }
uint16_t& GetTileId() { return this->_TileId; }
void SetTileId(uint16_t val) { this->_TileId = val; }
bool _IsPassable;
uint16_t _TileId;
void Serialize(OutputFileStream& ofs);
size_t TellSize();
size_t Unserialize(InputFileStream& ifs, size_t metadata = 0);
};
This error also occurs in a class where I have a property like in the Tile class where I use a std::shared_ptr<ISerializable>.
I guess interfaces don’t have compiler-generated copy constructors or assignment operators.
One possible solution is to explicitly implement
DerivedClass::operator=. That’s because the compiler-generated version will try to callISerializable::operator=, which doesn’t exist. Same thing for copy constructors.Another solution is to make all your classes COM classes 🙂
Example
Using your Tile class: