1

I am trying to declare a static class within a parent class and initialize it, but I seem to be getting all sorts of errors.

/* MainWindow.h */
    class MainWindow
    {
        private:
        static DWORD WINAPI threadproc(void* param);
        static MainWindow *hWin;
    };
/* MainWindow.cpp */
#include "MainWindow.h"
      void MainWindow::on_pushButton_clicked()
        {
            HANDLE hThread = CreateThread(NULL, NULL, threadproc, (void*) this, NULL, NULL);
            WaitForSingleObject(hThread, INFINITE);
            CloseHandle(hThread);
        }

        DWORD WINAPI MainWindow::threadproc(void* param)
        {
            hWin = (MainWindow*) param;
            //Be able to access stuff like hWin->run();
            return 0;
        }

I have tried using MainWindow::hWin = (MainWindow*) param; and MainWindow::hWin = new MainWindow((MainWindow*) param)); and many others, but none seem to work. What is the proper way to do this? Are there any resources anybody would recommend on this subject, I have been tangling with class problems for a few days now and am very frustrated.

2
  • 1
    What error messages do you get? Commented Feb 23, 2012 at 12:41
  • There's no static classes in C++. (What you have is a static data member.) Commented Feb 23, 2012 at 12:43

2 Answers 2

4

Static members always consist of a declaration and a definition, you lack the definition in your cpp file. Put the following line outside of any functions:

MainWindow* MainWindow::hWin;

You can read more here or here.

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

5 Comments

I get: error: C2655: 'MainWindow::hWin' : definition or redeclaration illegal in current scope,error: C2086: 'MainWindow *MainWindow::hWin' : redefinition when I use your code.
Updated the answer: You need to put the definition outside of any functions.
I don't understand why C++ would be so picky as to demand it outside of a function. I will read the links you posted, thanks.
"Always" isn't quite accurate - you don't need a definition of an integer constant that's never odr-used. But you certainly do need one in this case.
@user99545: Anything defined inside a code block is scoped inside that block, and is different to anything with the same name that's declared in a class, a namespace, or a different block.
0

Using a static variable like in your example will not allow you to have more than one instance, so it's best to avoid it if possible. And in your example there is no need to use one, you can easily use a local variable instead.

Just remove the static MainWindow *hWin; from your class definition, and modify MainWindow::threadproc() to use a local variable:

    DWORD WINAPI MainWindow::threadproc(void* param)
    {
        MainWindow* const hWin = static_cast<MainWindow*>(param);
        //hWin->whatever();
        return 0;
    }

However, if you really want to/have to use a static variable (for reasons that are not apparent in your example), then I'd suggest to set it in MainWindow's ctor - and just there. No need to explicitly pass it to the thread.

    MainWindow::MainWindow()
    {
        assert(hWin == 0);
        hWin = this;
    }

    MainWindow::~MainWindow()
    {
        assert(hWin == this);
        hWin = 0;
    }

    void MainWindow::on_pushButton_clicked()
    {
        HANDLE hThread = CreateThread(0, 0, threadproc, 0, 0, 0);
        WaitForSingleObject(hThread, INFINITE);
        CloseHandle(hThread);
    }

    DWORD WINAPI MainWindow::threadproc(void*)
    {
        // just use hWin, it already points to the one and only MainWindow instance
        return 0;
    }

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.