Qt,无法在窗口的构造函数中实例化对象?

时间:2015-01-01 00:38:06

标签: c++ qt

如何在窗口的构造函数中创建对象的实例?我只是通过在' window.h中声明一个名为对象的指针来生成三个错误。并在' window.cpp中实例化它:' ' Window :: Window(...){... 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)

debug\Phursik.exe:-1: error: LNK1120: 2 unresolved externals

我查看了错误,显然它们与声明的函数有关,但没有被类定义。我确定;但是,所有在' objectHandler.h'中声明的函数都是在' objectHandler.cpp'中定义和Qt Creator甚至知道如何从另一个中找到一个。我很困惑,所以,谢谢你的帮助。

来自window.cpp

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

来自window.h

namespace Ui {
class Window;
}

class Window : public QWidget
{
    Q_OBJECT

public:
    explicit Window(QWidget *parent = 0);
    ~Window();
...

来自objecthandler.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;
}...

来自objecthandler.h

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

1 个答案:

答案 0 :(得分:1)

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

Why doesn't Qt Creator find included headers in included paths - even though qmake is able to find them