我正在寫服務器是多線程的客戶端服務器程序,我想用while()在我的線程中創建循環,但它在myhthread.cpp中得到以下錯誤: 「之前‘ID’預期‘)’」我知道我的問題是基本的,但我真的很困惑......我怎麼能爲它創建循環這裏是我的代碼:?錯誤:'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);
}
在CPP你的構造函數的定義210
即使offtopic,但它也可能是http://blog.qt.io/blog/2010/06/17 /你做錯了/適合你。 'QTcpSocket'是默認的異步,所以爲什麼把它放在一個線程中(你在做它?) – Zaiborg