无法使用移动/复制构造函数时如何移动或深度复制?

时间:2019-05-27 09:16:36

标签: c++ copy-constructor move-semantics deep-copy move-constructor

我希望进行深度复制或将对象移动到std :: vector中,但我无法这样做。在查看了类的实现之后,他们没有了复制/移动构造函数。使用隐式move构造函数无济于事,因为对象被破坏了,并且我遇到了异常。

Exception thrown at 0x00007FF96D780BE2 (snap7.dll) in SeimensPLC.exe: 0xC0000005: Access violation writing location 0x0000000000000000.

Unhandled exception at 0x00007FF96D780BE2 (snap7.dll) in SeimensPLC.exe: 0xC0000005: Access violation writing location 0x0000000000000000.

实施类定义(plc.hpp):

class plc
{
private:
    std::vector<TS7Client>      client;   //doesn't works
    std::array<TS7Client,255>   clients;  //works
}

代码:

bool plc::connect_plc(const std::string& ip, std::uint8_t connectionType, std::uint16_t rack, std::uint16_t slot)
{
    try {
        TS7Client client;

        if (std::int32_t returnVal = client.SetConnectionType(connectionType)) //PG-PC : Programming Console type connection
        {
            LOG_ERROR << SrvErrorText(returnVal);
            return false;
        }
        if (std::int32_t returnVal = client.ConnectTo(ip.c_str(), rack, slot) not_eq EXIT_SUCCESS)
        {
            LOG_ERROR << SrvErrorText(returnVal);
            return false;
        }
        this->client.push_back(std::move(client));
        return true;
    }
    catch (const std::exception& ex)
    {
        LOG_FATAL << "Exception : " << ex.what();
    }
    return false;
}

bool plc::add_plc(const std::string& ip, const std::vector<config_table_struct>& config_list)
{

    if(!this->connect_plc(ip))
        return false;
    TS7CpuInfo cpu_info;
    this->client.at(0).GetCpInfo(&cp_info);
    this->client.at(0).GetCpuInfo(&cpu_info);
    spdlog::critical("CP Info : Max PDU Length : {}\nMax Connection : {}\nMax MPI bus : {}\nMax Bus Rate",
        cp_info.MaxPduLengt, cp_info.MaxConnections, cp_info.MaxMpiRate, cp_info.MaxBusRate);
    spdlog::critical("CPU Info : Module Type Name : {}\nSerial Number : {}\nAS Name : {}\nModule Name",
        cpu_info.ModuleName, cpu_info.SerialNumber, cpu_info.ASName, cpu_info.ModuleName);// <--- I get exception on this line
    return true;
}

库类定义:

class TS7Client
{
private:
    S7Object Client;
public:
    TS7Client();
    ~TS7Client();
    int Connect();
    int ConnectTo(const char *RemAddress, int Rack, int Slot);
    int Disconnect();
};
  

构建环境:x64(_WIN64)

typedef unsigned __int64  uintptr_t;  //vadefs.h
typedef uintptr_t  S7Object;

注意-我不想使用std :: array,因为所需的连接对象(客户端)的数量仅在读取数据库后才知道。该数字永远不会达到255。

0 个答案:

没有答案