gpt4 book ai didi

c++ - "Attempting to reference a deleted function"添加 QMutex 到类后

转载 作者:行者123 更新时间:2023-11-30 03:22:33 25 4
gpt4 key购买 nike

我正在使用 Qt5 构建应用程序。我的程序构建并运行良好,但访问数据结构的两个线程之间存在冲突。我有一个 CanMessage 对象的 QList,我想使用 QMutex 保护其中的一些数据。但是,一旦我将 QMutex 添加到我的类定义中,就会出现错误:

QList.h: `error: C2280: 
'CanMessage::CanMessage(const CanMessage &)': attempting to reference a deleted function`.

这是我的canmessage.h 文件:

#ifndef CANMESSAGE_H
#define CANMESSAGE_H

#include <QObject>
#include <QMutex>
#include "cansignal.h"

class CanMessage
{
public:
CanMessage();
/* snip - public function prototypes */

private:
/* snip - private prototypes and data */
QMutex m_messageMutex;
};

#endif // CANMESSAGE_H

cansignal.h:

#ifndef CANSIGNAL_H
#define CANSIGNAL_H

#include <QObject>
#include <QDebug>
#include <QByteArray>

class CanSignal
{
public:
CanSignal();

CanSignal(QString &signalName, quint8 &signalLength, quint8 &signalStartBit,
float &offset, float &factor, bool &isBigEndian, bool &isFloat, bool &isSigned)
{
this->m_name = signalName;
this->m_length = signalLength;
this->m_startBit = signalStartBit;
this->m_offset = offset;
this->m_factor = factor;
this->m_isBigEndian = isBigEndian;
this->m_isFloat = isFloat;
this->m_isSigned = isSigned;
}

bool setName(QString &signalName);
bool setBitLength(quint8 &length);
bool setStartBit(quint8 &startBit);
bool setOffset(float &offset);
bool setFactor(float &factor);
bool setEndianess(bool &isBigEndian);
bool setIsFloat(bool &isFloat);
bool setIsSigned(bool &isSigned);
void setValid();
void setInvalid();
void setEngineeringData(float data);

QString getName();
quint8 getBitLength();
quint8 getStartBit();
float getOffset();
float getFactor();
float getData();
bool isBigEndian();
bool isFloat();
bool isSigned();
bool getSignalValidity();


private:
QString m_name;
quint8 m_length;
quint8 m_startBit;
float m_offset;
float m_factor;
float m_data_float = 0;
bool m_isBigEndian;
bool m_isFloat;
bool m_isSigned;
// Set After everything in signal is filled
bool m_isSignalValid = false;
};

#endif // CANSIGNAL_H

最佳答案

CanMessage::CanMessage(const CanMessage &)

是复制构造函数,显然用于将项目放入列表中。这是行不通的,因为 QMutex 不是实际上可复制的。

如何解决它取决于很多因素。也许最简单的方法是修改 CanMessage,使其具有动态互斥体(当然是在构造函数中创建的)。

然后为其创建一个复制构造函数,该构造函数首先锁定 对象互斥量,然后在目标对象中动态分配一个 互斥量。

那样的话,你可以保证旧对象在复制时是“干净的”(因为你有它的互斥量)并且不会有“试图复制一个不可复制的成员”的问题,因为互斥量本身不是 已复制。有关详细信息,请参见脚注 (a)


以下代码是显示问题的完整简单片段,如果您将 QMutex m_mutex; 行注释掉,则可以正常编译:

#include <QList>
#include <QMutex>

#include <iostream>

class Xyzzy {
private:
int m_xyzzy;
//QMutex m_mutex;
public:
Xyzzy() : m_xyzzy(0) {};
Xyzzy(int val) : m_xyzzy(val) {};
};

int main() {
QList<Xyzzy> myList;
Xyzzy plugh;
myList.push_back(plugh);
return 0;
}

一旦您取消注释该行,编译器就会正确地提示:

 error: use of deleted function 'Xyzzy::Xyzzy(const Xyzzy&)'

(a) 在解决问题方面,您可以执行以下操作:

#include <QList>
#include <QMutex>

#include <iostream>

class Xyzzy {
private:
int m_xyzzy;
QMutex *m_mutex; // Now a pointer
public:
Xyzzy() : m_xyzzy(0) {
m_mutex = new QMutex(); // Need to create in constructor.
std::cout << "constructor " << m_mutex << '\n';
};
~Xyzzy() {
std::cout << "destructor " << m_mutex << '\n';
delete m_mutex; // Need to delete in destructor.
}
Xyzzy(const Xyzzy &old) {
old.m_mutex->lock();
m_mutex = new QMutex(); // Need to make new one here.
std::cout << "copy constructor from " << old.m_mutex
<< " to " << m_mutex << '\n';
old.m_mutex->unlock();
}
};

int main() {
QList<Xyzzy> myList;
Xyzzy plugh;
myList.push_back(plugh);
return 0;
}

根据以下测试运行,该设备正常工作:

constructor 0x21c9e50
copy constructor from 0x21c9e50 to 0x2fff2f0
destructor 0x21c9e50
destructor 0x2fff2f0

在实际代码中,我可能会选择智能指针而不是原始的 new/delete 调用,但这只是为了说明这个概念。此外,您需要处理所有其他可能性,这些可能性根据三/五/接下来发生的规则从现有对象创建新对象,目前(从内存中)仅限于复制分配成员 Xyzzy &operator=(const Xyzzy &old).

关于c++ - "Attempting to reference a deleted function"添加 QMutex 到类后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51053139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com