0

我在编译 dialog.h 时遇到问题,编译器抱怨 QHostAddress::Any 不是类型,并且是数字常量之前的预期标识符。(都在 dialog.h 的倒数第二行)。

有人能告诉我为什么这不会编译吗?我正在实例化服务器对象,并传递服务器构造函数期望的参数......我想......

对话框.h

#include <QWidget>
#include <QHostAddress>
#include "server.h"

class QLabel;
class QPushButton;

class Dialog : public QWidget
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);

private:
    QLabel *statusLabel;
    QPushButton *quitButton;
    Server server;
};

服务器.h:

class Server : public QTcpServer
{
    Q_OBJECT

public:
    Server(QHostAddress listenAddress, quint16 listenPort, QObject *parent = 0);
    QHostAddress hostAddress;
    quint16 hostPort;

protected:
    void incomingConnection(qintptr socketDescriptor);

private:

};

对话框.cpp(部分)

Dialog::Dialog(QWidget *parent)
    : QWidget(parent), server(QHostAddress::Any, 4000)
{

server.cpp(部分)

#include "server.h"
#include "clientthread.h"

#include <stdlib.h>
Server(QHostAddress listenAddress, quint16 listenPort, QObject *parent = 0)
    : hostAddress(listenAddress), hostPort(listenPort), QTcpServer(parent)
{
}

注意上面的代码已更新。现在编译器抱怨:

服务器的构造函数定义上的“listenAddress”之前应为“)”。

4

1 回答 1

0

您需要将 Server 对象声明为 Dialog 类成员变量,然后在构造函数中定义它。以下是 Dialog 类的外观:

对话框.h

#include <QWidget>
#include <QHostAddress>
#include "server.h"

class QLabel;
class QPushButton;

class Dialog : public QWidget
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);

private:
    QLabel *statusLabel;
    QPushButton *quitButton;
    Server server; // Declare server member variable.
};

对话框.cpp

Dialog::Dialog(QWidget *parent)
:
    QWidget(parent),
    server(QHostAddress::Any, 4000) // construct server
{
    //...
}
于 2013-09-28T21:37:51.157 回答