I am using Code::Blocks 12.11 on Windows 8
I am following the tutorial here - Lesson 2 - Creating a basic window - Natural Language Processing, London and I am receiving the error "undefined reference to 'MainWindow::m_hInstance'" in the MainWindow.cpp file. I don't know enough about the C++ language to guage whether there is an error in the code, or (more likely) it's an error in the Code::Blocks project set-up.
I have three files: main.cpp, MainWindow.cpp, MainWindow.h
main.cpp
Code:
#include <windows.h>
#include "MainWindow.h"
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
MainWindow *winMain = new MainWindow(hInst);
if(!winMain->Run(nCmdShow))
{
delete winMain;
return 1;
}
// Run the message loop. It will run until GetMessage() returns 0
while (GetMessage (&msg, NULL, 0, 0))
{
// Translate virtual-key messages into character messages
TranslateMessage(&msg);
// Send message to WindowProcedure
DispatchMessage(&msg);
}
delete winMain;
return msg.wParam;
}
MainWindow.cpp
Code:
#include <windows.h>
#include "MainWindow.h"
char MainWindow::m_szClassName[] = "DrawLite";
MainWindow::MainWindow(HINSTANCE hInstance)
{
m_hInstance = hInstance;
m_wndClass.cbSize = sizeof(WNDCLASSEX);
m_wndClass.style = CS_DBLCLKS;
m_wndClass.lpfnWndProc = MainWndProc;
m_wndClass.cbClsExtra = 0;
m_wndClass.cbWndExtra = 0;
m_wndClass.hInstance = hInstance;
m_wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wndClass.hCursor = LoadIcon(NULL, IDC_ARROW);
m_wndClass.hbrBackground = (HBRUSH) COLOR_WINDOW;
m_wndClass.lpszMenuName = NULL;
m_wndClass.lpszClassName = m_szClassName;
m_wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
}
MainWindow::~MainWindow()
{
}
LRESULT CALLBACK MainWindow::MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
return 0;
}
bool MainWindow::Run(int nCmdShow)
{
if(!RegisterClassEx(&m_wndClass))
return false;
m_hwnd = CreateWindowEx(
0,
m_szClassName,
"Draw Lite",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
m_hInstance,
NULL
);
if(!m_hwnd)
return false;
ShowWindow(m_hwnd, nCmdShow);
return true;
}
MainWindow.h
Code:
#ifndef MAINWINDOW_H_INCLUDED
#define MAINWINDOW_H_INCLUDED
#include <windows.h>
class MainWindow
{
public:
MainWindow(HINSTANCE hInstance);
~MainWindow();
static LRESULT CALLBACK MainWndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool Run(int nCmdShow);
private:
WNDCLASSEX m_wndClass;
static HINSTANCE m_hInstance;
HWND m_hwnd;
static char m_szClassName[];
};
#endif // MAINWINDOW_H_INCLUDED
I have searched Google for undefined reference problems and have found a few posts which were solved by adding the files to the project - I have added the files by right clicking the project and choosing "add files" and making sure all the boxes are ticked.
I have also tried to add the project directry to the linker tab as described in the Codeblocks manual, and then tried to add the object file in the same way.
I have got the feeling that it is an error in the project set-up, but I'm out of ideas.