gpt4 book ai didi

c++ - Qt 线程出错

转载 作者:行者123 更新时间:2023-11-30 02:45:58 25 4
gpt4 key购买 nike

在构建以下代码行时,我遇到了一些奇怪的错误:(我包括 QVector 和 QThread)。

节点进程.h:

class NodeProcess : public QThread
{ public:
NodeProcess();

unsigned int create_NewProcess(int priority);
NodeProcess operator[](int relativeID); private:
int priority;
unsigned int ID;
unsigned int threadCount;

QVector<NodeProcess> mProcess;

};

节点进程.cpp:

NodeProcess::NodeProcess()
{
threadCount = 0;
}

unsigned int NodeProcess::create_NewProcess(int priority){
this->priority = priority;

threadCount++;
NodeProcess newProcess;
mProcess.append(newProcess);
return threadCount;
}

NodeProcess NodeProcess::operator[](int relativeID)
{
return mProcess[relativeID];
}

有人知道我做错了什么吗?任何提示都会帮助我 :)。

In file included from ..\..\..\..\5.2.1\mingw48_32\include/QtCore/qalgorithms.h:45:0,
from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/qvector.h:45,
from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/QVector:1,
from ..\NodeProcessModelling\nodeprocess.h:4,
from ..\NodeProcessModelling\nodeprocess.cpp:1:
..\..\..\..\5.2.1\mingw48_32\include/QtCore/qobject.h: In copy constructor 'QThread::QThread(const QThread&)':
..\..\..\..\5.2.1\mingw48_32\include/QtCore/qobject.h:465:20: error: 'QObject::QObject(const QObject&)' is private
Q_DISABLE_COPY(QObject)
^
..\..\..\..\5.2.1\mingw48_32\include/QtCore/qglobal.h:978:5: note: in definition of macro 'Q_DISABLE_COPY'
Class(const Class &) Q_DECL_EQ_DELETE;\
^
In file included from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/QThread:1:0,
from ..\NodeProcessModelling\nodeprocess.h:5,
from ..\NodeProcessModelling\nodeprocess.cpp:1:
..\..\..\..\5.2.1\mingw48_32\include\QtCore/qthread.h:57:21: error: within this context
class Q_CORE_EXPORT QThread : public QObject
^
In file included from ..\NodeProcessModelling\nodeprocess.cpp:1:0:
..\NodeProcessModelling\nodeprocess.h: In copy constructor 'NodeProcess::NodeProcess(const NodeProcess&)':
..\NodeProcessModelling\nodeprocess.h:7:7: note: synthesized method 'QThread::QThread(const QThread&)' first required here
class NodeProcess : public QThread
^
..\NodeProcessModelling\nodeprocess.cpp: In member function 'NodeProcess NodeProcess::operator[](int)':
..\NodeProcessModelling\nodeprocess.cpp:19:31: note: synthesized method 'NodeProcess::NodeProcess(const NodeProcess&)' first required here
return mProcess[relativeID];
^
In file included from ..\..\..\..\5.2.1\mingw48_32\include/QtCore/qalgorithms.h:45:0,
from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/qvector.h:45,
from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/QVector:1,
from ..\NodeProcessModelling\nodeprocess.h:4,
from ..\NodeProcessModelling\nodeprocess.cpp:1:
..\..\..\..\5.2.1\mingw48_32\include\QtCore/qthread.h: In instantiation of 'void QVector<T>::append(const T&) [with T = NodeProcess]':
..\NodeProcessModelling\nodeprocess.cpp:13:31: required from here
..\..\..\..\5.2.1\mingw48_32\include/QtCore/qglobal.h:979:12: error: 'QObject& QObject::operator=(const QObject&)' is private
Class &operator=(const Class &) Q_DECL_EQ_DELETE;
^
..\..\..\..\5.2.1\mingw48_32\include/QtCore/qobject.h:465:5: note: in expansion of macro 'Q_DISABLE_COPY'
Q_DISABLE_COPY(QObject)
^
In file included from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/QThread:1:0,
from ..\NodeProcessModelling\nodeprocess.h:5,
from ..\NodeProcessModelling\nodeprocess.cpp:1:
..\..\..\..\5.2.1\mingw48_32\include\QtCore/qthread.h:57:21: error: within this context
class Q_CORE_EXPORT QThread : public QObject
^
In file included from ..\NodeProcessModelling\nodeprocess.cpp:1:0:
..\NodeProcessModelling\nodeprocess.h:7:7: note: synthesized method 'QThread& QThread::operator=(const QThread&)' first required here
class NodeProcess : public QThread
^
In file included from ..\..\..\..\5.2.1\mingw48_32\include\QtCore/QVector:1:0,
from ..\NodeProcessModelling\nodeprocess.h:4,
from ..\NodeProcessModelling\nodeprocess.cpp:1:
..\..\..\..\5.2.1\mingw48_32\include\QtCore/qvector.h:569:19: note: synthesized method 'NodeProcess& NodeProcess::operator=(const NodeProcess&)' first required here
*d->end() = copy;
^
Makefile.Debug:227: recipe for target 'debug/nodeprocess.o' failed
mingw32-make[1]: *** [debug/nodeprocess.o] Error 1
mingw32-make[1]: Leaving directory 'C:/Qt/Tools/QtCreator/bin/build-NodeProcessModelling-Desktop_Qt_5_2_1_MinGW_32bit-Debug'
makefile:34: recipe for target 'debug' failed
mingw32-make: *** [debug] Error 2
18:03:35: The process "C:\Qt\Tools\mingw48_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project NodeProcessModelling (kit: Desktop Qt 5.2.1 MinGW 32bit)
When executing step 'Make'

最佳答案

Someone any idea what I'm doing wrong ?

很多东西!

首先,如果您要使用 QThread,请不要继承它,除非您确实打算更改 QThread 管理线程的方式。 QThread 更像是一个线程 Controller ,而不是实际的线程本身。

您需要做的是创建您的类并从 QObject 派生,然后将其移至新的 QThread。您可以阅读有关如何 "Really Truly Use QThreads" here 的信息并使用示例代码作为模板。

您还创建了 NodeProcess 实例的 QVector。这会调用复制构造函数,但对象派生自 QObject,其复制构造函数是私有(private)的。这就是您收到此错误的原因:-

error: 'QObject::QObject(const QObject&)' is private

您需要使用节点进程指针的 QVector:QVector< NodeProcess* > 并根据需要分配它们。

关于c++ - Qt 线程出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24020067/

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