简体   繁体   English

使用unique_ptr <>实现移动构造函数和赋值

[英]Implementing move constructor and assignment with unique_ptr<>

I have the current constructor in my Device.cpp file 我的Device.cpp文件中有当前构造函数

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

My Device.h contains a class Device with: 我的Device.h包含一个具有以下内容的设备类:

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. 我试图纠正移动构造函数并移动赋值,因为unique_ptr无法复制,因此我的类变为不可复制,并且〜Device()最终将其删除。

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: 我在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 使用此库用于BTSerialPortBinding在Visual Studio 2015上的Windows 10中运行: https : //github.com/Agamnentzar/bluetooth-serial-port

unique_ptr cannot be copied and any class containing it cannot be copy-constructed or copy-assigned. unique_ptr不能被复制,包含它的任何类都不能被复制构造或复制分配。 You will need to define at least move constructor and move assignment operator for your class. 您将需要至少为类定义move构造函数和move赋值运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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