GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2007, 11:18
The Great Dane The Great Dane is offline
New Member
 
Join Date: Nov 2007
Posts: 2
The Great Dane is on a distinguished road

"Linker Error" problem with C++


Hi, guys. I just recently bought a new programming book called "Beginning Game Programming." The first major project to do in the book is to create a simple Game Engine. In the book, it is broken up into two seperate files: GameEngine.cpp and GameEngine.h. Well, I aside from a few errors in the books code I finally got it to almost compile properly in Dev-C++. The only problem I am having now is I have 9 "Linker error"s. I don't have any idea of what these are or how to fix them. Any help would be great.

Here are the errors:
[Linker error] undefined reference to `GameInitialize(HINSTANCE__*)'
[Linker error] undefined reference to `GameCycle()'
[Linker error] undefined reference to `GameEnd()'
[Linker error] undefined reference to `GameStart(HWND__*)'
[Linker error] undefined reference to `GameActivate(HWND__*)'
[Linker error] undefined reference to `GameDeactivate(HWND__*)'
[Linker error] undefined reference to `GamePaint(HDC__*)'
[Linker error] undefined reference to `GameEnd()'
ld returned 1 exit status


Here is the source code for both files:

CPP / C++ / C Code:
GameEngine.cpp:
//-----------------------------------------------------------------
// Game Engine Object
// C++ Source - GameEngine.cpp
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "GameEngine.h"

//-----------------------------------------------------------------
// Static Variable Initialization
//-----------------------------------------------------------------
GameEngine *GameEngine::m_pGameEngine = NULL;

//-----------------------------------------------------------------
// Windows Functions
//-----------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  PSTR szCmdLine, int iCmdShow)
{
  MSG         msg;
  static int  iTickTrigger = 0;
  int         iTickCount;

  if (GameInitialize(hInstance))
  {
    // Initialize the game engine
    if (!GameEngine::GetEngine()->Initialize(iCmdShow))
      return FALSE;

    // Enter the main message loop
    while (TRUE)
    {
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
        // Process the message
        if (msg.message == WM_QUIT)
          break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      else
      {
        // Make sure the game engine isn't sleeping
        if (!GameEngine::GetEngine()->GetSleep())
        {
          // Check the tick count to see if a game cycle has elapsed
          iTickCount = GetTickCount();
          if (iTickCount > iTickTrigger)
          {
            iTickTrigger = iTickCount +
              GameEngine::GetEngine()->GetFrameDelay();
            GameCycle();
          }
        }
      }
    }
    return (int)msg.wParam;
  }

  // End the game
  GameEnd();

  return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
  // Route all Windows messages to the game engine
  return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
}

//-----------------------------------------------------------------
// GameEngine Constructor(s)/Destructor
//-----------------------------------------------------------------
GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass,
  LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight)
{
  // Set the member variables for the game engine
  m_pGameEngine = this;
  m_hInstance = hInstance;
  m_hWindow = NULL;
  if (lstrlen(szWindowClass) > 0)
    lstrcpy(m_szWindowClass, szWindowClass);
  if (lstrlen(szTitle) > 0)
    lstrcpy(m_szTitle, szTitle);
  m_wIcon = wIcon;
  m_wSmallIcon = wSmallIcon;
  m_iWidth = iWidth;
  m_iHeight = iHeight;
  m_iFrameDelay = 50;   // 20 FPS default
  m_bSleep = TRUE;
}

GameEngine::~GameEngine()
{
}

//-----------------------------------------------------------------
// Game Engine General Methods
//-----------------------------------------------------------------
BOOL GameEngine::Initialize(int iCmdShow)
{
  WNDCLASSEX    wndclass;

  // Create the window class for the main window
  wndclass.cbSize         = sizeof(wndclass);
  wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  wndclass.lpfnWndProc    = WndProc;
  wndclass.cbClsExtra     = 0;
  wndclass.cbWndExtra     = 0;
  wndclass.hInstance      = m_hInstance;
  wndclass.hIcon          = LoadIcon(m_hInstance,
    MAKEINTRESOURCE(GetIcon()));
  wndclass.hIconSm        = LoadIcon(m_hInstance,
    MAKEINTRESOURCE(GetSmallIcon()));
  wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  wndclass.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  wndclass.lpszMenuName   = NULL;
  wndclass.lpszClassName  = m_szWindowClass;

  // Register the window class
  if (!RegisterClassEx(&wndclass))
    return FALSE;

  // Calculate the window size and position based upon the game size
  int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
      iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2       + GetSystemMetrics(SM_CYCAPTION);
  if (wndclass.lpszMenuName != NULL)
    iWindowHeight += GetSystemMetrics(SM_CYMENU);
  int iXWindowPos = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2,
      iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2;

  // Create the window
  m_hWindow = CreateWindow(m_szWindowClass, m_szTitle,  WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX, iXWindowPos, iYWindowPos, iWindowWidth, iWindowHeight, NULL, NULL, m_hInstance, NULL);
  if (!m_hWindow)
    return FALSE;

  // Show and update the window
  ShowWindow(m_hWindow, iCmdShow);
  UpdateWindow(m_hWindow);

  return TRUE;
}

LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
  // Route Windows messages to game engine member functions
  switch (msg)
  {
    case WM_CREATE:
      // Set the game window and start the game
      SetWindow(hWindow);
      GameStart(hWindow);
      return 0;

    case WM_SETFOCUS:
      // Activate the game and update the Sleep status
      GameActivate(hWindow);
      SetSleep(FALSE);
      return 0;

    case WM_KILLFOCUS:
      // Deactivate the game and update the Sleep status
      GameDeactivate(hWindow);
      SetSleep(TRUE);
      return 0;

    case WM_PAINT:
      HDC         hDC;
      PAINTSTRUCT ps;
      hDC = BeginPaint(hWindow, &ps);

      // Paint the game
      GamePaint(hDC);

      EndPaint(hWindow, &ps);
      return 0;

    case WM_DESTROY:
      // End the game and exit the application
      GameEnd();
      PostQuitMessage(0);
      return 0;
  }
  return DefWindowProc(hWindow, msg, wParam, lParam);
}




And, here is the GameEngine.h Source code:

CPP / C++ / C Code:
#ifndef GAMEENGINE_H
#define GAMEENGINE_H

#include <windows.h>


//Windows Function Declaration
int WINAPI        WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     PSTR szCmdLine, int iCmdShow);
LRESULT CALLBACK  WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

//Game Engine Function Declarations
BOOL GameInitialize(HINSTANCE hInstance);
void GameStart(HWND hWindow);
void GameEnd();
void GameActivate(HWND hWindow);
void GameDeactivate(HWND hWindow);
void GamePaint(HDC hDC);
void GameCycle();

//Game Engine Class
class GameEngine
{
   protected:
//Member Variables
     static GameEngine*  m_pGameEngine;
     HINSTANCE           m_hInstance;
     HWND                m_hWindow;
     TCHAR               m_szWindowClass[32];
     TCHAR               m_szTitle[32];
     WORD                m_wIcon, m_wSmallIcon;
     int                 m_iWidth, m_iHeight;
     int                 m_iFrameDelay;
     BOOL                m_bSleep;
     
public:
        //Constructor/Deconstructor
        GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPSTR szTitle,
           WORD wIcon, WORD wSmallIcon, int iWidth = 640, int iHeight = 480);
        virtual ~GameEngine();
        
        //General Methods
        static GameEngine*   GetEngine() { return m_pGameEngine; };
        BOOL                 Initialize(int iCmdShow);
        LRESULT              HandleEvent(HWND hWindow, UINT msg, WPARAM wParam,
                                LPARAM lParam);
                                
        //Accessor Methods
        HINSTANCE    GetInstance()   { return m_hInstance; };
        HWND         GetWindow()     { return m_hWindow; };
        void         SetWindow(HWND hWindow) { m_hWindow = hWindow; };
        LPTSTR       GetTitle()      { return m_szTitle; };
        WORD         GetIcon()       { return m_wIcon; };
        WORD         GetSmallIcon()  { return m_wSmallIcon; };
        int          GetWidth()      { return m_iWidth; };
        int          GetHeight()     { return m_iHeight; };
        int          GetFrameDelay() { return m_iFrameDelay; };
        void         SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 /
                        iFrameRate; };
        BOOL         GetSleep()      { return m_bSleep; };
        void         SetSleep(BOOL bSleep) { m_bSleep = bSleep; };
};

#endif


So, any help would be greatly appreciated. Thanks.
  #2  
Old 10-Nov-2007, 12:01
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: "Linker Error" problem with C++


Quote:
Originally Posted by The Great Dane
[Linker error] undefined reference to `GameInitialize(HINSTANCE__*)'
[Linker error] undefined reference to `GameCycle()'
[Linker error] undefined reference to `GameEnd()'
[Linker error] undefined reference to `GameStart(HWND__*)'
[Linker error] undefined reference to `GameActivate(HWND__*)'
[Linker error] undefined reference to `GameDeactivate(HWND__*)'
[Linker error] undefined reference to `GamePaint(HDC__*)'
[Linker error] undefined reference to `GameEnd()'
These functions aren't defined anywhere in the code you posted. So, you need to find them somewhere in the book, write the code and link / add that file to your project.

Nothing else we can do, except if someone has the same book.

Errors in the book code, however, are never a really good sign...
__________________
Music, programming, endless learning..
  #3  
Old 10-Nov-2007, 16:55
The Great Dane The Great Dane is offline
New Member
 
Join Date: Nov 2007
Posts: 2
The Great Dane is on a distinguished road

Re: "Linker Error" problem with C++




Okay, I see what you're saying, and I found out what the problem was. Since the code was made as a game engine specifically it wasn't meant to be compiled and run. So, it was meant to be used with the next chapter's program. The functions are actually defined in the other program that goes with it. Though, they didn't specify this in the book, so I didn't know.

Though, now I'm having a problem with the first program that is made to go with this engine. I will post the code for it in a little bit and the problem.

Thanks a lot for the help. Even though you probably didn't intend to help me in that way, you actually helped me understand what I personally didn't get. So, thanks a lot
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Buffer problem with CD-RWriter KieranC Computer Software Forum - Windows 2 07-Jan-2006 01:45
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
String problem vaha C Programming Language 3 24-May-2005 18:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.