1

How do I create an instance of an object in the constructor for a window? I am generating three errors just by declaring a pointer, named objects, in 'window.h' and instantiating it in '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

I looked up the errors and apparently they have to do with the functions being declared but not defined by the class. I am sure; however, that all functions declared in 'objectHandler.h' are defined in 'objectHandler.cpp' and Qt Creator even knows how to find one from the other. I am quite perplexed so thank you for your help in advance.

From window.cpp

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

From window.h

namespace Ui {
class Window;
}

class Window : public QWidget
{
    Q_OBJECT

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

From 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;
}...

From objecthandler.h

class objectHandler
{
public:
    objectHandler(int stepTimeHours);
    ~objectHandler();
...
    largeBody *head, *current, *tail;
}
5
  • It looks like a mistake in the #includes. There is nothing special with instantiating objects inside a window's constructor. Do you use Q_OBJECT in your custom classes? Commented Jan 1, 2015 at 0:43
  • If you deliberately insert a syntax error (e.g. by typing in garbage characters) into either of those two functions (i.e. into objecthandler class's constructor or destructor), do you then get a syntax error when you rebuild your program? Commented Jan 1, 2015 at 4:20
  • "It looks like a mistake in the #includes. There is nothing special with instantiating objects inside a window's constructor. Do you use Q_OBJECT in your custom classes?" I added in redundancy and checked the includes so I don't know why it would be. I only use Q_OBJECT in my window class. "If you deliberately insert a syntax error (e.g. by typing in garbage characters) into either of those two functions (i.e. into objecthandler class's constructor or destructor), do you then get a syntax error when you rebuild your program?" I do not. Commented Jan 1, 2015 at 7:07
  • @user3732350 : could you post some of the more relevant code? For example, the header of your custom class, and at least the beginning of your cpp files? Commented Jan 1, 2015 at 12:28
  • I added it to the O.P. Commented Jan 1, 2015 at 22:26

1 Answer 1

1

I solved it. The issue below is similar except QT Creator automagically added my '.h' files to the list in the .pro file. I just needed to delete the build folder and suddenly everything started working.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.