Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用QTcpServer时程序崩溃_C++_Qt_Syntax Error_Runtime Error_Qtcpserver - Fatal编程技术网

C++ 调用QTcpServer时程序崩溃

C++ 调用QTcpServer时程序崩溃,c++,qt,syntax-error,runtime-error,qtcpserver,C++,Qt,Syntax Error,Runtime Error,Qtcpserver,我正在尝试一个非常非常简单的QT网络程序。由于某些原因,它在执行时崩溃,没有任何错误消息,因为它没有按预期将任何输出输出输出到命令行。代码如下: qtTCPservertest.pro QT += core QT += network QT -= gui TARGET = qtTCPservertest CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main

我正在尝试一个非常非常简单的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
服务器.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
#ifndef THESERVER_H
#define THESERVER_H

#include <QTcpServer>

class TheServer : public QTcpServer
{
    Q_OBJECT
public:
    TheServer(QObject *pParent = nullptr);
    void goOnline();
};

#endif // THESERVER_H
main.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");
}
#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();
}
#include <QDebug>
#include "TheServer.h"

TheServer::TheServer(QObject *pParent)
    : QTcpServer(pParent)
{
}

void TheServer::goOnline()
{
    bool status = listen(QHostAddress::Any, 5200);

    if (status) {
        qDebug() << "Server up";
    } else {
        qDebug() << "Server down";
    }
}
#include <QCoreApplication>
#include "TheServer.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TheServer server;
    server.goOnline();

    return a.exec();
}
#包括
#包括
#包括“theserver.h”
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
printf(“测试”);
theServer*aServer=新建theServer();
aServer->goOnline();
aServer->~theServer();
返回a.exec();
}
有人知道我哪里出错了吗?既然没有错误,我就一窍不通。它只是不打印任何东西,它只是告诉我按任何键来关闭窗口,就像它像往常一样结束一样


感谢您的建议。

以下是为我编译并运行的代码(Qt 5.5):

服务器.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
#ifndef THESERVER_H
#define THESERVER_H

#include <QTcpServer>

class TheServer : public QTcpServer
{
    Q_OBJECT
public:
    TheServer(QObject *pParent = nullptr);
    void goOnline();
};

#endif // THESERVER_H
#如果卸载服务器#
#定义服务器
#包括
类服务器:公共QTcpServer
{
Q_对象
公众:
服务器(QObject*pParent=nullptr);
void goOnline();
};
#endif//THESERVER\u H
服务器.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");
}
#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();
}
#include <QDebug>
#include "TheServer.h"

TheServer::TheServer(QObject *pParent)
    : QTcpServer(pParent)
{
}

void TheServer::goOnline()
{
    bool status = listen(QHostAddress::Any, 5200);

    if (status) {
        qDebug() << "Server up";
    } else {
        qDebug() << "Server down";
    }
}
#include <QCoreApplication>
#include "TheServer.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TheServer server;
    server.goOnline();

    return a.exec();
}
#包括
#包括“TheServer.h”
服务器::服务器(QObject*pParent)
:qtcserver(pParent)
{
}
作废服务器::goOnline()
{
bool status=listen(QHostAddress::Any,5200);
如果(状态){

qDebug()你试过在debug中运行它吗?@cen现在我在Qt/Creator中使用gdb时遇到了问题,因为我认为修复这个问题会是一项更大的工作,所以我无法调试它(如果你是说:
gdb
)我建议您在Qt Creator中修复调试器,因为至少这样您就可以确切地知道程序崩溃的位置。一旦您知道这应该会更容易。如果没有打印“测试”,您将遇到更大的网络问题。您的theServer类必须是QObject(缺少Q_OBJECT宏)@Archie是的,没错,但还是一样。我尝试了另一种选择:我的类仅从“public QObject”继承,我声明了一个类似
qtcserver*aServer
的服务器,直到这里一切都顺利运行,如果我插入初始化,错误就会返回
aServer=new qtcserver(此)
如果无效调用该构造函数,id并不重要。这就是为什么我仍然认为这是网络问题。首先:感谢您的耐心和帮助。这并不能真正解决我的问题,但它确实表明我走在正确的轨道上,代码也很好。我没有预料到这一点,但现在我相信QTNetwo存在问题rk库,可能丢失或不兼容。因此我必须调试这里提供的QT设置,因为它不是一个通用的shelve@user3085931确保加载正确的Qt库(例如,如果您有多个Qt安装或某些依赖于Qt的程序将自身放入路径中)。在Windows上,您可以使用dependency walker()查看启动可执行文件时实际加载了哪些DLL。