Qt,不能在窗口的构造函数中实例化对象吗?

Qt, cannot instantiate an object in a window's constructor?

本文关键字:实例化 对象 构造函数 不能 窗口 Qt      更新时间:2023-10-16

如何在窗口的构造函数中创建对象的实例? 我通过在"window.h"中声明一个指针(命名对象)并在"窗口.cpp:"窗口中::窗口(...)"中实例化它来生成三个错误{...objects = new objectHandler(1)}'

window.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall objectHandler::objectHandler(int)" (??0objectHandler@@QAE@H@Z) referenced in function "public: __thiscall Window::Window(class QWidget *)" (??0Window@@QAE@PAVQWidget@@@Z)
(file not found)
window.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall objectHandler::~objectHandler(void)" (??1objectHandler@@QAE@XZ) referenced in function "public: void * __thiscall objectHandler::`scalar deleting destructor'(unsigned int)" (??_GobjectHandler@@QAEPAXI@Z)
(file not found)
debugPhursik.exe:-1: error: LNK1120: 2 unresolved externals

我查找了错误,显然它们与正在声明但未由类定义的函数有关。 我敢肯定;但是,在"objectHandler.h"中声明的所有函数都是在"objectHandler.cpp"中定义的,Qt Creator甚至知道如何从另一个函数中找到一个函数。 我很困惑,所以提前感谢您的帮助。

从窗口.cpp

Window::Window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Window)
{
...
    objects = new objectHandler(STEP_TIME_HOURS);
    ui->setupUi(this);
}

从窗口.h

namespace Ui {
class Window;
}
class Window : public QWidget
{
    Q_OBJECT
public:
    explicit Window(QWidget *parent = 0);
    ~Window();
...

从对象处理程序.cpp

objectHandler::objectHandler(int stepTimeHours)
{
    this->stepTimeHours = stepTimeHours;
    head = nullptr;
    current = nullptr;
    tail = nullptr;
}
objectHandler::~objectHandler()
{
    current = head;
    if (current->next)
    {
        current = current->next;
        delete current->last;
    }
    else if (current)
        delete current;
}...

来自对象处理程序.h

class objectHandler
{
public:
    objectHandler(int stepTimeHours);
    ~objectHandler();
...
    largeBody *head, *current, *tail;
}

我解决了。 下面的问题类似,除了 QT Creator 自动将我的".h"文件添加到 .pro 文件中的列表中。 我只需要删除构建文件夹,突然一切都开始工作。

为什么Qt Creator在包含的路径中找不到包含的标头 - 即使qmake能够找到它们