Reputation: 179
I'm writing client server program that server is multi thread, I want to use while() for creating loop in my thread , but it get the following error in myhthread.cpp: "expected ')' before 'ID'" I know my problem is basic but i am really confused about it... how can i create loop for it?Here is my code:
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);
}
Upvotes: 1
Views: 576
Reputation: 73446
Use scope resolution in the definition of your constructor in the cpp, and get rid of the :
at the end of the line:
mythread::mythread(qintptr ID, QObject *parent) // :
{
...
}
Without the mythread::
prefix, the compiler understands that you want to declare some object of type mythread
and is confused by the syntax.
Edit: as Danh noted, once you have corrected the error, the compiler will draw your attention that you have two definitions of the same constructor, which is illegal.
Possible correction: you should clean your class declaration in the header from all function implementation, and move implementation to the cpp file. As you have two different implementations of the constructor, you could try to merge both :
//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()
{
...
}
Upvotes: 1