2015-12-15 36 views
1

我正在寫服務器是多線程的客戶端服務器程序,我想用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
+0

即使offtopic,但它也可能是http://blog.qt.io/blog/2010/06/17 /你做錯了/適合你。 'QTcpSocket'是默認的異步,所以爲什麼把它放在一個線程中(你在做它?) – Zaiborg

回答

1

使用範圍解析,並獲得該行的末尾擺脫:的:

mythread::mythread(qintptr ID, QObject *parent) // : 
{ 
    ... 
} 

沒有mythread::前綴,編譯器明白你想聲明一些對象的類型爲mythread,並且語法混淆。

編輯:正如Danh指出的,一旦你糾正了錯誤,編譯器會提醒你注意你有兩個相同構造函數的定義,這是非法的。

可能的更正:您應該從所有函數實現中清除頭中的類聲明,並將實現移至cpp文件。正如你所構造的兩種不同的實現,你可以嘗試合併兩種:

//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() 
{ 
    ... 
} 
+1

實際上,他定義了2個定義mythread :: mythread(qintptr ID,QObject * parent)'我猜他會回來了新的錯誤。 – Danh

+0

@Danh好點!一旦錯誤得到糾正,他會得到一個關於重新定義的明確錯誤消息。 – Christophe

+0

是的,你猜對了.. 請問我該怎麼辦? – Hanita

相關問題