首页 > 解决方案 > 收到错误“未定义对 MainWindow 的引用”

问题描述

我正在尝试编写一个函数,如果那里有有效的出口,则允许我的角色在游戏中向北行驶。当我运行程序时,它给了我这个错误。我在 ZorkUL.cpp 中编写了函数,并在主窗口中包含了 ZorkUL 头文件

undefined reference to `MainWindow::go(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

这是我的代码。任何帮助将不胜感激谢谢

ZorkUL.h

#define ZORKUL_H_

#include "Command.h"
#include "Parser.h"
#include "Room.h"
#include "item.h"
#include <iostream>
#include <string>
using namespace std;

class ZorkUL {
private:
    Parser parser;
    Room *currentRoom;
    void createRooms();

    bool processCommand(Command command);
    void printHelp();
    void goRoom(Command command);
    void createItems();
    void displayItems();
    //string go(string direction);

public:
    ZorkUL();
    void play();
    string printWelcome();
    string go(string direction);
};



#endif /*ZORKUL_H_*/

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Room.h"
#include "ZorkUL.h"
#include "Command.h"
#include "CommandWords.h"
#include "Parser.h"

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


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_northButton_clicked()
{
go("north");
ui->textBox->setText("You went north! You're now in room ");
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QListWidget>
#include "ZorkUL.h"
#include "Character.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();



private:
    void go(string direction);
    void goRoom(string direction);

private slots:
    void on_quitButton_clicked() {
        close();

    };

    void on_northButton_clicked();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

ZorkUL.cpp

#include <iostream>
#include <string>

using namespace std;
#include "ZorkUL.h"
#include "mainwindow.h"


ZorkUL::ZorkUL() {
    createRooms();
}

void ZorkUL::createRooms()  {
    Room *a, *b, *c, *d, *e, *f, *g, *h, *i, *j, *k, *l;

    a = new Room("a");
        a->addItem(new Item("sword", 1, 11));
        a->addItem(new Item("bow", 2, 22));
        a->addItem(new Item("axe", 2, 22));
    b = new Room("b");
    c = new Room("c");
    d = new Room("d");
    e = new Room("e");
    f = new Room("f");
    g = new Room("g");
        g->addItem(new Item("wand", 1, 1));
        g->addItem(new Item("armour" ,1,1));
    h = new Room("h");
    i = new Room("i");
    j = new Room("j");
    k = new Room("k");
    l = new Room("l");

//             (N, E, S, W)
    a->setExits(b, NULL, NULL, NULL);
    b->setExits(NULL, c, NULL, j);
    c->setExits(d, NULL, NULL, b);
    d->setExits(e, NULL, c, NULL);
    e->setExits(NULL, f, d, NULL);
    f->setExits(k, e, g, h);
    g->setExits(f, NULL, NULL, f);
    h->setExits(NULL, f, i, NULL);
    i->setExits(h,NULL, j, NULL);
    j->setExits(i, NULL, NULL, b);
    k->setExits(l, NULL, NULL, NULL);
    l->setExits(NULL,NULL,NULL,NULL);



        currentRoom = a;
}

string ZorkUL::go(string direction) {
    //Make the direction lowercase
    //transform(direction.begin(), direction.end(), direction.begin(),:: tolower);
    //Move to the next room
    Room* nextRoom = currentRoom->nextRoom(direction);
    if (nextRoom == NULL)
        return("direction null");
    else
    {
        currentRoom = nextRoom;
        return currentRoom->longDescription();
    }
} ```



标签: c++qt

解决方案


推荐阅读