Undefined Reference to a private HINSTANCE in the Header


Header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <windows.h>

class MainWindow
{
    public:
        MainWindow(HINSTANCE hInstance);

        virtual ~MainWindow();
    static LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg,  WPARAM wParam, LPARAM lParam); //Creates LRESULT CALLBACK That Calls Parameters if Run is true
    bool Run(int nCmdShow);

private:
    WNDCLASSEX m_wndclass;

    HWND m_hwnd;
     static HINSTANCE m_hInstance;
    static char m_szClassName[];
    };

#endif // MAINWINDOW_H 




.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MainWindow::MainWindow(HINSTANCE hInstance)
{
        m_hInstance = hInstance; // THIS IS WHERE ERROR IS//

    m_wndclass.cbClsExtra = 0;
    m_wndclass.cbSize = sizeof(WNDCLASSEX);
    m_wndclass.cbWndExtra = 0;
    m_wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW);
    m_wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
    m_wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    m_wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    m_wndclass.hInstance = hInstance;
    m_wndclass.lpfnWndProc = MainWndProc;
    m_wndclass.lpszClassName = m_szClassName;
    m_wndclass.style = CS_DBLCLKS;
    m_wndclass.lpszMenuName = NULL;

}

I really am not understanding where my error is....Im just trying to create a simple window. This just started today
Did you initialize m_hInstance in the cpp file?

HINSTANCE MainWindow::m_hInstance = NULL;
Yes. Making m_hInstance non static seemed to solve the problem but i dont really understand why...
Use this:
MainWindow::m_hInstance = hInstance; // THIS IS WHERE ERROR IS//

Static members does not have hidden 'this' pointer applied automatically.
Thats what i thought modoran. Tried it, didnt work, and i need that particular HINSTANCE to be static now so i cant make it nonstatic
You have to initialize the static variable outside the class definition. Did you add that line of code to the top of the cpp file? e.g.

1
2
3
4
5
6
7
8
9
#include "MainWindow.h"

HINSTANCE MainWindow::m_hInstance = NULL;

MainWindow::MainWindow(HINSTANCE hInstance)
{
    m_hInstance = hInstance; // should work
    ...
}
Last edited on
Topic archived. No new replies allowed.