我在编译dialog.h时遇到问题,编译器抱怨QHostAddress :: Any不是类型,而且是数字常量之前的预期标识符。 (都在dialog.h的倒数第二行)。
有人可以告诉我为什么这不会编译?我正在实例化服务器对象,并传递服务器构造函数所期望的参数......我想......
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;
};
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:
};
dialog.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'之前的Server的构造函数定义。
答案 0 :(得分:0)
您需要将Server对象声明为Dialog类成员变量,而不是在构造函数中定义它。以下是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.
};
dialog.cpp
Dialog::Dialog(QWidget *parent)
:
QWidget(parent),
server(QHostAddress::Any, 4000) // construct server
{
//...
}