Operator = is not a member of the exported C ++ __interface in a DLL

I wrote code for lib and experimented with the default Win32 console application to make everything work. Since I finished all the classes, I wanted to extract everything in the DLL, and so I started the adaptation with a regular macro:

#ifdef MYLIB_EXPORTS #define DllExport __declspec(dllexport) #else #define DllExport __declspec(dllimport) #endif 

I use one interface in my code, which is defined as follows:

 __interface DllExport ISerializable { void Serialize(/* ... */); /* some other methods */ }; 

And it worked when I used this code in my exe. In dll I get an error at compile time that says

 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 each class that inherits ISerializable to implement the necessary methods. (I use std::shared_ptr<ISerializable> several times to have an 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 am I getting this error? Why do I need an assignment operator for my class / interface in my DLL? Is there a workaround?

(Using RTM for Visual Studio 2012 in RTM for Windows 8 with C ++ 11.)


Here is one segment where this error occurs (the error always refers to the last } 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 the class where I have a property similar to the Tile class, where I use std::shared_ptr<ISerializable> .

+4
source share
1 answer

I assume that the interfaces do not have copy constructors or assignment operators created by the compiler.

One possible solution is to explicitly implement DerivedClass::operator= . This is because the version generated by the compiler will try to call ISerializable::operator= , which does not exist. The same goes for copy constructors.

Another solution is to create all classes of COM classes :)


Example

Using the Tile class:

 class DllExport Tile final : public ISerializable { public: Tile(const Tile& tile) : _IsPassable(tile._IsPassable), _TileId(tile._TileId) { } /* New Code START */ Tile& operator=(const Tile& tile) { _IsPassable = tile._IsPassable; _TileId = tile._TileId; return *this; } /* New Code END */ __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); }; 
+1
source

Source: https://habr.com/ru/post/1434493/


All Articles