Sign in to follow this  
jdub

Why won't this link

Recommended Posts

I am getting these errors trying to compile this code: 1>GameLibMain.obj : error LNK2001: unresolved external symbol "private: static class GameWindow * GameWindow::gameWnd" (?gameWnd@GameWindow@@0PAV1@A) 1>C:\Projects\GameLibrary\Src\GameLib\Release\GameLib.exe : fatal error LNK1120: 1 unresolved externals Source code:

#include <windows.h>
#include <d3d9.h>
#include <tchar.h>
#include <memory.h>
#include "GameWindow.h"

GameWindow::GameWindow(HINSTANCE instance , int nShowCmd)
{
	this->hInstance = instance;

	//Create and register the window class
	WNDCLASSEX wndClass;
	memset(&wndClass , 0 , sizeof( wndClass ));

	wndClass.cbSize = sizeof( wndClass );
	wndClass.lpfnWndProc = wndProc;
	wndClass.lpszClassName = _T("Game Window");
	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.hInstance = instance;
	
	RegisterClassEx(&wndClass);
	
	//Create the window itself
	this->hWnd = CreateWindowEx(0 , 
								_T("Game Window") , 
								_T("Game Window") , 
								WS_OVERLAPPEDWINDOW , 
								0 , 
								0 , 
								200 , 
								200 ,
								NULL ,
								NULL ,
								instance ,
								0);
	if (!this->hWnd)
	{
		return;
	}

	ShowWindow(this->hWnd , nShowCmd);	
}

GameWindow::~GameWindow()
{
	
}

void GameWindow::GameWindowMainLoop()
{
	MSG msg;

	while( GetMessage(&msg , this->hWnd , 0 , 0 ) )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

LRESULT CALLBACK GameWindow::wndProc(HWND inHwnd, UINT inMsg, WPARAM inWParam, LPARAM inLParam)
{
	switch(inMsg)
	{
	case WM_QUIT:
		PostQuitMessage(0);
	}

	return DefWindowProc(inHwnd , inMsg , inWParam , inLParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nShowCmd)
{
	
	GameWindow::CreateGameWindow(hInstance , nShowCmd);
	GameWindow::GetGameWindow()->GameWindowMainLoop();
	GameWindow::KillGameWindow();
}

Header file:

#ifndef GAME_LIB_MAIN_H
#define GAME_LIB_MAIN_H

class GameWindow
{
private:
	static GameWindow* gameWnd;
	HINSTANCE hInstance;
	HWND hWnd;
	
	static LRESULT CALLBACK wndProc(HWND inHwnd, UINT inMsg, WPARAM inWParam, LPARAM inLParam);

public:
	static GameWindow* GetGameWindow() { if (GameWindow::gameWnd == NULL){/* Throw an exeption here */} return gameWnd; }
	
	void GameWindowMainLoop();
	static void CreateGameWindow(HINSTANCE hInstance , int nShowCmd) { gameWnd = new GameWindow(hInstance , nShowCmd); }
	static void KillGameWindow() { delete gameWnd; } 
	GameWindow(HINSTANCE hInstance, int nShowCmd);
	~GameWindow();

};

#endif


What is happening here?

Share this post


Link to post
Share on other sites
For static member variables, you have to implement the actual variable outside the scope of the class declaration. Try adding:

GameWindow* GameWindow::gameWnd = NULL;




to your .cpp file, below the include for GameWindow.h

Hope this helps,
Matt Rudder

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this