PDA

View Full Version : windows Programming problem


GabrielBlumenthal
12-15-2002, 12:52 PM
here are the seperate files:

common.h:
class MANAGER
{
public:
void SetInstance(HINSTANCE);
HINSTANCE GetInstance() const;
private:
HINSTANCE Instance;
};

extern MANAGER* Manager;
window.h:
class WINDOW
{
friend LRESULT CALLBACK
WindowProc(HWND,UINT,WPARAM,LPARAM);
public:
static int Run();
static void Register();
static void Unregister();
void Create(LPCTSTR);
void Show();
private:
LRESULT Destroy(HWND,UINT,WPARAM,LPARAM);
HWND Window;
};

manager.cpp:
#include <windows.h>
#include "common.h"

void MANAGER::SetInstance(HINSTANCE instance)
{
Instance = instance;
}

HINSTANCE MANAGER::GetInstance() const
{
return Instance;
}

window.cpp:
#include <windows.h>
#include "window.h"
#include "common.h"

int WINDOW::Run()
{
MSG message;
while (GetMessage(&message,NULL,0,0))
DispatchMessage(&message);
return message.wParam;
}

void WINDOW::Register()
{
WNDCLASS windowclass;
LPCSTR icon,cursor;
HBRUSH brush;
icon = MAKEINTRESOURCE(IDI_APPLICATION);
cursor = MAKEINTRESOURCE(IDC_ARROW);
brush = (HBRUSH) GetStockObject(WHITE_BRUSH);
windowclass.lpszClassName = "windowclassname";
windowclass.hInstance = Manager->GetInstance();
windowclass.lpfnWndProc = WindowProc;
windowclass.style = CS_HREDRAW | CS_VREDRAW;
windowclass.lpszMenuName = NULL;
windowclass.hIcon = LoadIcon(NULL,icon);
windowclass.hCursor = LoadCursor(NULL,cursor);
windowclass.hbrBackground = brush;
windowclass.cbClsExtra = 0;
windowclass.cbWndExtra = 4;
RegisterClass(&windowclass);
}

void WINDOW::Unregister()
{
UnregisterClass("windowclassname",
Manager->GetInstance());
}

void WINDOW::Create(LPCSTR title)
{
DWORD style = WS_OVERLAPPEDWINDOW;
HMENU menu = NULL;
HWND parent = NULL;
LPVOID params = this;
Window = CreateWindow("windowclassname",
title,
style,
0,
0,
200,
150,
parent,
menu,
Manager->GetInstance(),
params);
}

LRESULT WINDOW: http://forums.massassi.net/html/biggrin.gifestroy(HWND window,
UINT message,
WPARAM wparam,
LPARAM lparam)
{
PostQuitMessage(0);
return 0;
}

main.cpp:
#include <windows.h>
#include "window.h"
#include "common.h"

MANAGER* Manager;

int WINAPI WinMain(HINSTANCE instance,
HINSTANCE,
LPSTR,
int)
{
Manager = (MANAGER*) new MANAGER;
Manager->SetInstance(instance);
WINDOW::Register();
WINDOW window;
window.Create("Test Program");
window.Show();
return WINDOW::Run();
}

LRESULT CALLBACK WindowProc(HWND window,
UINT message,
WPARAM wparam,
LPARAM lparam)
{
if (message == WM_CREATE)
{
CREATESTRUCT* createstruct;
createstruct = (CREATESTRUCT*)lparam;
SetWindowLong(window,
0,
(LONG) (createstruct->lpCreateParams));
return 0;
}
WINDOW* object = (WINDOW*) GetWindowLong(window,0);
if (object)
{
if (message == WM_DESTROY)
return object->Destroy(window,
message,
wparam,
lparam);
}
return DefWindowProc(window,
message,
wparam,
lparam);
}

They all compile fine, but when I try to create the executable, it says:
Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall WINDOW::Show(void)" (?Show@WINDOW@@QAEXXZ)
Debug/FRAMEWORK.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

FRAMEWORK.exe - 2 error(s), 0 warning(s)

umm... the smilie in it is supposd to be a "d", the name of the function is Destroy, yeah so there you go.

[This message has been edited by GabrielBlumenthal (edited December 15, 2002).]

lordvader
12-15-2002, 01:15 PM
You didn't actually define WINDOW::Show() anywhere, you only defined its prototype.

------------------
Capitalization (http://webster.commnet.edu/grammar/capitals.htm)
Commas (http://webster.commnet.edu/grammar/commas.htm)
Periods (http://webster.commnet.edu/grammar/marks/period.htm)
Question Marks (http://webster.commnet.edu/grammar/marks/question.htm)
Apostrophes (http://webster.commnet.edu/grammar/marks/apostrophe.htm)
Confusable Words (http://webster.commnet.edu/grammar/notorious.htm)
Plague Words (http://webster.commnet.edu/grammar/plague.htm)

GabrielBlumenthal
12-15-2002, 01:25 PM
Oops... http://forums.massassi.net/html/redface.gif
One question though: It compiled with no errors, are you sure it wouldn't just stop compiling, rather than showing a linker error. That seems to me like more of a compiling eror.

GabrielBlumenthal
12-15-2002, 01:30 PM
Just tried it, it worked, thanks lordvader.

lordvader
12-15-2002, 03:53 PM
It'd be a linker error, because there is indeed a definition for WINDOW::Show(), but the linker will then be unable to link it because it can't find the actual declaration of WINDOW::Show().

Gamma_Ray54
12-15-2002, 11:38 PM
The compiler operates on a file-by-file basis. If in one of your files, you tell it that a variable exists, or that a function is prototyped, it'll just accept that and happily compile your file. It doesn't have the scope to be able to look across multiple files and see that the function is not, in fact, actually defined anywhere. That's the linker's job. Therefore, linker error.