我在编译dialog.h时遇到了问题,编译器抱怨QHostAddress::Any不是类型,在数字常量之前是预期的标识符。(在对话的第二行到最后一行)。
有人能告诉我为什么这不能编译吗?我正在实例化服务器对象,并传递服务器构造器所期望的参数.我认为.
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;
};服务器。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”之前的“预期”。
发布于 2013-09-28 21:37:51
您需要将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
{
//...
}https://stackoverflow.com/questions/19071859
复制相似问题