如何修复Qt C ++中的“未定义引用”错误

时间:2019-05-21 19:11:07

标签: c++ qt

我正在开发包含共享库的游戏,并且在尝试编译项目时遇到了一个小问题。

我将此库链接到.pro文件:

#-------------------------------------------------
#
# Project created by QtCreator 2019-05-21T19:25:52
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = JanturiolGameServer
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        Janturiolgameserver.cpp

HEADERS += \
        Janturiolgameserver.h

FORMS += \
        Janturiolgameserver.ui

CONFIG += mobility
MOBILITY = 


# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-JanturiolLibrary-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/release/ -lJanturiolLibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-JanturiolLibrary-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/debug/ -lJanturiolLibrary
else:unix: LIBS += -L$$PWD/../build-JanturiolLibrary-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/ -lJanturiolLibrary

INCLUDEPATH += $$PWD/../JanturiolLibrary
DEPENDPATH += $$PWD/../JanturiolLibrary

这是放置在我库中的PlayerCharacter.h:

#ifndef PLAYERCHARACTER_H
#define PLAYERCHARACTER_H

#include"basecharacter.h"
#include <string>


enum CharacterClass{warrior, mage, ranger};

struct Position
{
    int xPos;
    int yPos;
};

class PlayerCharacter : public BaseCharacter
{
public:
    PlayerCharacter(std::string charName);
    virtual ~PlayerCharacter();

protected:
    std::string name;
    Position pos;
};

#endif // PLAYERCHARACTER_H

PlayerCharacter.cpp:

#include "playercharacter.h"
#include "string"

PlayerCharacter::PlayerCharacter(std::string charName)
{
    name = charName;
    pos.xPos = 0;
    pos.yPos = 0;
}


PlayerCharacter::~PlayerCharacter()
{
}


This is project main window .h :

#ifndef JANTURIOLGAMESERVER_H
#define JANTURIOLGAMESERVER_H

#include <QMainWindow>
#include "qudpsocket.h"

namespace Ui {
class JanturiolGameServer;
}

class PlayerCharacter;

enum datagramContext{msCommand ,pAction};

class JanturiolGameServer : public QMainWindow
{
    Q_OBJECT

public:
    JanturiolGameServer(QWidget *parent = Q_NULLPTR);
    ~JanturiolGameServer() override;

private:
    Ui::JanturiolGameServer* ui;

    QString name;
    QUdpSocket* server;
    QHostAddress mainServerAdress;
    quint16 mainServerPort;
    int maxPlayersCount;
    int playersCount;
    PlayerCharacter** players;

    void ProcessDatagram(QByteArray &data);

    void AddNewPlayer(PlayerCharacter* newPlayer);

    void closeEvent(QCloseEvent* clEvent) override;

private slots:
    void RegisterGameServer();
    void ShutDownServer();

public slots:
    void readyRead();
};


#endif // JANTURIOLGAMESERVER_H

这是项目主窗口.cpp:

  #include "Janturiolgameserver.h"
    #include "ui_Janturiolgameserver.h"
    #include "playercharacter.h"

    JanturiolGameServer::JanturiolGameServer(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::JanturiolGameServer)
    {
        ui->setupUi(this);

        server = new QUdpSocket();
        server->bind(QHostAddress::LocalHost, 1234);
        connect(server, SIGNAL(readyRead()), this, SLOT(readyRead()));
        name = QString::null;
        mainServerAdress = QHostAddress::LocalHost;
        mainServerPort = 1234;
        maxPlayersCount = 150;
        playersCount = 0;
        players = new PlayerCharacter* [maxPlayersCount];

        //connect(Ui.startServer_but, SIGNAL(released()), this, SLOT(RegisterGameServer()));
        //connect(Ui.shutDown_but, SIGNAL(released()), this, SLOT(ShutDownServer()));
    }

    JanturiolGameServer::~JanturiolGameServer()
    {
        delete[] players;
        delete server;
    }

    void JanturiolGameServer::ProcessDatagram(QByteArray& data)
    {
        if (data[0] == 'm')
        {

        }
        if (data[0] == 'a')
        {
            int dataSize;
            dataSize = data.size();

            std::string playerName;

            for (int i = 3; i < dataSize; i++)
            {
                playerName.push_back(data[i]);
            }
//there is where the problem causes
            PlayerCharacter* newPlayer;
            newPlayer = new PlayerCharacter(playerName);

            AddNewPlayer(newPlayer);
        }
    }

    void JanturiolGameServer::AddNewPlayer(PlayerCharacter* newPlayer)
    {
        players[playersCount] = newPlayer;
        playersCount++;
    }

    void JanturiolGameServer::closeEvent(QCloseEvent* clEvent)
    {
        ShutDownServer();
        QMainWindow::closeEvent(clEvent);
    }

    void JanturiolGameServer::RegisterGameServer()
    {
        if (name == QString::null)
            return;

        QByteArray regData;
        regData.append("rs " + name);
        server->writeDatagram(regData ,mainServerAdress, mainServerPort);
    }

    void JanturiolGameServer::ShutDownServer()
    {
        QByteArray regData;
        //shut down server
        regData.append("sd " + name);
        server->writeDatagram(regData, mainServerAdress, mainServerPort);
    }

    void JanturiolGameServer::readyRead()
    {
        QByteArray buffer;
        buffer.resize(server->pendingDatagramSize());

        QHostAddress dataSender;
        quint16 senderPort;
        server->readDatagram(buffer.data(), buffer.size(), &dataSender, &senderPort);

        ProcessDatagram(buffer);
    }

当我尝试构建项目时,出现此错误:

enter image description here

0 个答案:

没有答案