您好,我正在尝试构建对象QVector
,QThread
但是当我尝试构建时出现此错误'QObject::QObject' : cannot access private member declared in class 'QObject'
。有人可以告诉我为什么我会收到这个错误以及如何克服它或指出我的答案方向。谢谢你的时间。
主程序
#include <QCoreApplication>
#include "thread.h"
#include <QDebug>
#include <QThread>
main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
unsigned long long startingNumberAnswer = 0;
unsigned long long totalIterationsAnswer = 0;
int numberOfThreads = 10;
QVector<Thread> threads(numberOfThreads);
for(int l = 0; l < threads.size(); l++ ){
threads[l].setPriority(QThread::TimeCriticalPriority);
}
for(int i = 1; i< 2000000; i+=numberOfThreads){
qDebug() << "Longest iteration Number = " << startingNumberAnswer;
qDebug() << "Number of iterations for " << startingNumberAnswer << " is " << totalIterationsAnswer;
qDebug() << "Running # " << i;
system("CLS");
}
qDebug() << "Longest iteration Number = " << startingNumberAnswer;
qDebug() << "Number of iterations for " << startingNumberAnswer << " is " << totalIterationsAnswer;
return a.exec();
}
线程.H
#ifndef THREAD_H
#define THREAD_H
#include <QObject>
#include <QThread>
class Thread : public QThread
{
Q_OBJECT
public:
explicit Thread();
unsigned long long getloops();
unsigned long long getnumber();
signals:
public slots:
void run(unsigned long long value);
private:
unsigned long long largestNumber;
unsigned long long loops;
unsigned long long number;
};
#endif // THREAD_H
线程.CPP
#include "thread.h"
Thread::Thread()
{
}
void Thread::run(unsigned long long value)
{
unsigned long long n = value;
unsigned long long counter = 0;
while ( n > 1){
if(n%2 == 0){
n = n/2;
} else {
n = (3*n) + 1;
}
counter++;
}
loops = counter;
number = value;
}
unsigned long long Thread::getloops(){
return loops;
}
unsigned long long Thread::getnumber(){
return number;
}