-2

In my Qt5.7 program, I'm trying to implement a solution from this post (@vz0 answer). I want to access my MainWindow class function from Settings class, but I'm getting an linker error. Please tell me, what am I doing wrong?

Fragment of:

main:

MainWindow w; //MainWindow declaration
a.setActivationWindow(&w);
w.setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);

mainwindow.h:

public:    
static MainWindow* getInstance() { return &mainwindowInstance; }
void trayReload();
void tray();

private:
static MainWindow mainwindowInstance;

mainwindow.cpp:

void MainWindow::trayReload()
{
delete trayIcon;
MainWindow::tray();
}

settings.cpp:

void Settings::bar()
{
MainWindow* mainWin = MainWindow::getInstance();
mainWin->trayReload(); //I want to make this working
}

And I'm getting this error after compiling this code:

settings.obj:-1: error: LNK2001: unresolved external symbol "private: static class MainWindow MainWindow::mainwindowInstance" (?mainwindowInstance@MainWindow@@0V1@A)

What should I do in this situation to make it working?

Community
  • 1
  • 1
km2442
  • 779
  • 2
  • 11
  • 31
  • Please post real code. The code you posted won't compile, and you won't get a linker error using the code you posted. – IInspectable Jul 23 '16 at 19:24
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Jul 23 '16 at 19:35
  • @IInspectable, as you wish, I edited my thread, now this is real code – km2442 Jul 23 '16 at 19:40
  • This isn't real code. [It won't compile](http://ideone.com/1i5yNP). Better diagnostics [using GCC 5.1](http://ideone.com/StZi4N). – IInspectable Jul 23 '16 at 19:47
  • sorry, I forgot about one static in example code, now [it's compiling](http://ideone.com/cLg8vo), but I still have a linker error in Qt – km2442 Jul 23 '16 at 20:00

1 Answers1

1

Your usage of a static class member is incomplete. Have a look at this line in your header:

static MainWindow mainwindowInstance;

All this does is declare the mainwindowInstance. You need to define it though (instantiate it). You can do this in your mainwindow.cpp:

MainWindow MainWindow::mainwindowInstance;

Note that you cannot do this in the header file, as then you would end up with multiple instances.

I suggest a completely different design though: Store the instance via a pointer in the class, have a setter for the instance and create it in main(), so you can also delete it in main() afterwards.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • Please don't answer questions, that are [**exact** duplicates](http://stackoverflow.com/a/12574407/1889329). As a judicious moderator, you are asked to vote to close the question instead. – IInspectable Jul 23 '16 at 20:33
  • Ok, how can I do it property? When I add MainWindow MainWindow::mainwindowInstance;, compiler says, that QWidget: Must construct a QApplication before a QWidget and when I place it in Widget constructor compiler says that this is an redefinition. – km2442 Jul 23 '16 at 20:33
  • That's why I suggested to use a setter and create it in `main()`. You can then follow the Qt guide's way of first creating an application, then the window. – ypnos Jul 23 '16 at 21:06