c++ - 错误 : expected ')' before 'ID'

标签 c++ qt while-loop

我正在编写服务器是多线程的客户端服务器程序,我想使用 while() 在我的线程中创建循环,但它在 myhthread.cpp 中出现以下错误: “在‘​​ID’之前需要‘)’” 我知道我的问题很简单,但我真的很困惑...我如何为它创建循环?这是我的代码:

我的线程.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

我的线程.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 的构造函数定义中使用范围解析,并去掉行尾的 ::

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

如果没有 mythread:: 前缀,编译器会理解您要声明一些类型为 mythread 的对象,并且会对语法感到困惑。

编辑:正如 Danh 指出的那样,一旦您更正了错误,编译器就会提醒您注意您对同一个构造函数有两个定义,这是非法的。

可能的更正:您应该从所有函数实现中清除 header 中的类声明,并将实现移至 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() 
 {
     ...
 }

关于c++ - 错误 : expected ')' before 'ID' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34282092/

相关文章:

c++ - 使用 Qt 表单中按顺序命名的 ui 对象填充数组

Java iterator.hasNext() 始终为真

R循环直到条件匹配,然后继续

c++ - CUDA:将统一内存与类和数组一起使用

c++ - 如何在 QGraphicsScene 中放置 QFrame?

c++ - 为什么 `boost::multi_array_ref` 的析构函数是非虚拟的?

c++ - Qt 5.0 与 MingW?

linux - (loopcount=loopcount+1) 和 ((loopcount=loopcount+1)) 有什么区别?

C++ 文件中的最后一个字符

java - Eclipse 问题 View 双击