【发布时间】:2016-03-20 03:58:24
【问题描述】:
我正在尝试一个非常非常简单的 QT 网络程序。由于某种原因,它在没有任何错误消息的情况下执行时崩溃,因为它没有按预期将任何输出打印到命令行。代码如下:
qtTCPservertest.pro
QT += core
QT += network
QT -= gui
TARGET = qtTCPservertest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
theserver.cpp
HEADERS += \
theserver.h
theServer.h
#ifndef THESERVER_H
#define THESERVER_H
#include <QTcpServer>
#include <stdio.h>
class theServer : public QTcpServer{
Q_OBJECT
public:
theServer();
~theServer();
void goOnline();
};
#endif // THESERVER_H
theServer.cpp
#include "theserver.h"
theServer::theServer()
{
}
theServer::~theServer()
{
}
void theServer::goOnline()
{
bool status = false;
unsigned int portNum = 5200;
status = this->listen(QHostAddress::Any, portNum );
// Check, if the server did start correctly or not
if( status == true )
printf("Server up\n");
else
printf("Server down\n");
}
和 main.cpp
#include <QCoreApplication>
#include <stdio.h>
#include "theserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("Test\n");
theServer* aServer = new theServer();
aServer->goOnline();
aServer->~theServer();
return a.exec();
}
有人知道我哪里出错了吗?由于没有错误,我完全一无所知。它只是没有打印出任何东西,它只是告诉我按任意键关闭窗口,就好像它像往常一样结束了。
感谢您的建议。
【问题讨论】:
-
您是否尝试在调试中运行它?
-
@cen 现在我在将 gdb 与 Qt/Creator 一起使用时遇到问题,因为我相信修复这将是一项更大的工作,所以我无法调试它(如果你的意思是例如:@987654325 @通过那个)
-
我建议你在 Qt Creator 中修复调试器,因为至少你会知道程序崩溃的确切位置。一旦你知道它应该更容易。如果没有打印“测试”,那么网络问题会更大。
-
您的 theServer 类必须是 QObject(缺少 Q_OBJECT 宏)
-
@Archie 没错,但还是一样。我尝试了另一种选择:我的类仅从“公共 QObject”继承,并且我声明了像
QTcpServer* aServer这样的服务器,直到这里一切都运行顺利,如果我插入初始化错误返回aServer = new QTcpServer(this)并且 id 无关紧要无效地调用该构造函数。这就是为什么我仍然认为是网络问题
标签: c++ qt syntax-error runtime-error qtcpserver