简体   繁体   中英

Implementing move constructor and assignment with unique_ptr<>

I have the current constructor in my Device.cpp file

Device::Device(const char *devName)
{
    device = devName;
    bt.reset(BTSerialPortBinding::Create(devName, 1));
}

My Device.h contains a class Device with:

Device(const char *devName="");
~Device();
const char *device;
std::unique_ptr<BTSerialPortBinding> bt;

I am trying to right a move constructor and move assignment as unique_ptr is not copyable so my class becomes non-copyable and the ~Device() ends up deleting it.

Hence when I try to use:

Device dev; // declared in Process.h

dev = Device("93:11:22"); // initialised in Process.cpp

I get the following error:

Device &Device::operator =(const Device &)': attempting to reference a deleted function

I've tried the following with no luck in Device.h:

//move assignment operator
Device &operator=(Device &&o)
{
    if (this != &o)
    {
        bt = std::move(o.bt);
    }
    return *this;
}
Device(Device &&o) : bt(std::move(o.bt)) {};

I get these errors when I try this:

1>bluetoothserialport.lib(BTSerialPortBinding.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in ArduinoDevice.obj
1>bluetoothserialport.lib(BluetoothHelpers.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in ArduinoDevice.obj
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) already defined in libcpmtd.lib(xlock.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) already defined in libcpmtd.lib(xlock.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "void __cdecl std::_Debug_message(wchar_t const *,wchar_t const *,unsigned int)" (?_Debug_message@std@@YAXPB_W0I@Z) already defined in libcpmtd.lib(stdthrow.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "void __cdecl std::_Xbad_alloc(void)" (?_Xbad_alloc@std@@YAXXZ) already defined in libcpmtd.lib(xthrow.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error@std@@YAXPBD@Z) already defined in libcpmtd.lib(xthrow.obj)
1>msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "void __cdecl std::_Xout_of_range(char const *)" (?_Xout_of_range@std@@YAXPBD@Z) already defined in libcpmtd.lib(xthrow.obj)

Running in Windows 10 on Visual Studio 2015, using this library for BTSerialPortBinding: https://github.com/Agamnentzar/bluetooth-serial-port

unique_ptr cannot be copied and any class containing it cannot be copy-constructed or copy-assigned. You will need to define at least move constructor and move assignment operator for your class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM