私はクライアントのサーバープログラムを書いていますマルチスレッドでは、私のスレッドでループを作成するためにwhile()を使いたいが、myhthread.cppに次のエラーが出る。 "expected") "before" "ID" " 私は私の問題が基本的だと知っていますが、私は本当にそれについて混乱しています...どのように私はそれのためのループを作成することができます?ここに私のコードです:
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include "myserver.h"
#include <QDebug>
class mythread : public QThread
{
Q_OBJECT
public:
mythread(qintptr ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
}
void run()
{
qDebug() << " Thread started";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
// if (m_client)
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
qDebug() << socketDescriptor << " Client connected";
exec();
}
signals:
void error(QTcpSocket::SocketError socketerror);
public slots:
void readyRead();
void disconnected();
private:
QTcpSocket* socket;
qintptr socketDescriptor;
};
#endif
mythread.cpp:
#include "mythread.h"
#include "myserver.h"
mythread(qintptr ID, QObject *parent) :
{
while(disconnected)
{
mythread::run();
}
}
void mythread::readyRead()
{
QByteArray Data = socket->readAll();
qDebug()<< " Data in: " << Data;
socket->write(Data);
}
void mythread::disconnected()
{
qDebug() << " Disconnected";
socket->deleteLater();
exit(0);
}
回答:
回答№1は1cpp内のコンストラクタの定義でスコープ解決を使用し、 :
行の最後に:
mythread::mythread(qintptr ID, QObject *parent) // :
{
...
}
なし mythread::
接頭辞の場合、コンパイラは型のオブジェクトを宣言することを理解しています mythread
構文によって混乱します。
編集: ダンが指摘しているように、エラーを訂正すると、コンパイラは同じコンストラクタの2つの定義を持つことに注意しますが、これは違法です。
可能な修正: クラス宣言をヘッダをすべての関数の実装から削除し、実装をcppファイルに移動します。コンストラクタの2つの異なる実装があるので、両方をマージすることができます:
//in the header: no implementation of functions
class mythread : public QThread
{
...
mythread(qintptr ID, QObject *parent);
void run();
...
};
// in the cpp
mythread(qintptr ID, QObject *parent)
: QThread(parent)
{
this->socketDescriptor = ID;
while(disconnected) // if you really want to do this during the construction
{
mythread::run();
}
}
void mythread::run()
{
...
}