CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    14

    [RESOLVED] error C2065: undeclared identifier

    hi all, this is my first post here so i'll try to be as clearly as possible;
    im not programmer but im trying to write a simple game in c++ so i have copyd some code from a book but that resulted in some errors regarding identifiers...
    here is the code:

    //.h file: (gameengine.h)
    pragma once

    class GameEngine
    {
    protected:
    //lidvariabelen
    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;

    //constructor/destructor
    public:
    GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth = 640, int iHeight = 480);
    virtual ~GameEngine();

    //alg methoden
    static GameEngine* GetEngine() {return m_pGameEngine;};
    BOOL Initialize(int iCmdShow);
    LRESULT HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);

    //accessor methodes
    HINSTANCE GetInstance(){return hInstance;};
    HWND GetWindow(){return m_hWindow;};
    void SetWindow(HWND hWindow){m_hWindow = hWindow;};
    LPSTR 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 iFameRate){m_iFrameDelay = 1000/ iFrameRate;};
    BOOL GetSleep(){return m_bSleep;};
    void SetSleep(BOOL bSleep){m_bSleep = bSleep;};

    };

    //
    GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth , int iHeight)
    {
    //lidvariabelen vr de gameengine instellen
    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;
    m_bSleep = TRUE;
    }

    GameEngine::~GameEngine()
    {
    }

    BOOL GameEngine::Initialize(int iCmdShow)
    {
    WNDCLASSEX wndclass;

    //vensterklasse vr hoofdvenster maken
    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;

    //vensterklasse registreren
    if (!RegisterClassEx(&wndclass))
    return FALSE;

    //venstergr en pos berekenen adhv grootte game
    int iWindowWidth = m_iWidth + GetSytemMetrics(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;
    //venster maken
    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;

    //venster weergeven en bijwerken
    ShowWindow(m_hWindow, iCmdShow);
    UpdateWindow(m_hWindow);

    return TRUE;

    }

    LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    //windowsberichten nr lidfuncties van game-engine sturen
    switch(msg)
    {
    case WM_CREATE:
    //gamevenster instellen en game starten
    SetWindow(hWindow);
    GameStart(hWindow);
    return 0;

    case WM_ACTIVATE:
    //game activeren/deact en pauzestatus bijwerken
    if (wParam != WA_INACTIVE)
    {
    GameActivate(hWindow);
    SetSleep(FALSE);
    }
    else
    {
    GameDeactivate(hWindow);
    SetSleep(TRUE);
    }
    return 0;

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

    //game weergeven
    GamePaint(hDC);

    EndPaint(hWindow, &ps);
    return 0;

    case WM_DESTROY:
    //game stoppen en toepassing afsluiten
    GameEnd();
    PostQuitMessage(0);
    return 0;
    }
    return DefWindowProc (hWindow, msg, wParam, lParam);
    }

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


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

    //berichtus starten
    while(TRUE)
    {
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    //bericht verwerken
    if (msg.message == WM_QUIT)
    break;
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    else
    {
    //controleren of engine niet is gepauzeerd
    if (GameEngine::GetEngine()->GetSleep())
    {
    //n ticks controleren om te zien of er een gamecyclus is verstreken
    iTickCount = GetTickCount();
    if (iTickCount > iTickTrigger)
    {
    iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay();
    GameCycle();
    }
    }
    }

    }
    return (int)msg.wParam;
    }
    //game stoppen
    GameEnd();
    return TRUE;
    }

    //.h file: (skeleton.h)

    #pragma once
    #include <windows.h>
    #include "Resource.h"
    #include "GameEngine.h"
    GameEngine* _pGame;

    //.cpp file (skeleton.cpp)

    #include "Skeleton.h"

    BOOL GameInitialize(HINSTANCE hInstance)
    {
    _pGame = new GameEngine(hInstance, TEXT("game"), TEXT("game"), IDI_SKELETON, IDI_SKELETON_SM);
    if(_pGame == NULL)
    return FALSE;

    _pGame->SetFrameRate(15);
    return TRUE;
    }

    void GameStart()
    {
    srand(GetTickCount());
    }

    void GameEnd()
    {
    delete _pGame;
    }

    void GameActivate(HWND hWindow)
    {
    HDC hDC;
    RECT rect;

    GetClientRect(hWindow, &rect);
    hDC = GetDC(hWindow);
    DrawText(hDC, TEXT("activated!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    ReleaseDC(hWindow, hDC);
    }

    void GameDeactivate(HWND hWindow)
    {
    HDC hDC;
    RECT rect;

    GetClientRect(hWindow, &rect);
    hDC = GetDC(hWindow);
    DrawText(hDC, TEXT("deactivated!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    ReleaseDC(hWindow, hDC);
    }

    void GamePaint(HDC hDC)
    {
    }

    void GameCycle()
    {
    HDC hDC;
    HWND hWindow = _pGame->GetWindow();

    hDC = GetDC(hWindow);
    DrawIcon(hDC, rand()% _pGame->GetWidth(), rand()% _pGame->GetHeight(), (HICON)(WORD)GetClassLong(hWindow, GCL_HICON));
    ReleaseDC(hWindow, hDC);
    }

    Sorry for the long code!! but then you'll get the whole picture...
    i have 20 errors all saying:
    Error 1 error C2065: 'hInstance' : undeclared identifier c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 28
    Error 2 error C2065: 'iFrameRate' : undeclared identifier c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 37
    Error 3 error C2065: 'wndProc' : undeclared identifier c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 73
    Error 4 error C3861: 'GetSytemMetrics': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 89
    Error 5 error C3861: 'Createwindow': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 94
    Error 6 error C3861: 'GameStart': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 114
    Error 7 error C3861: 'GameActivate': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 121
    Error 8 error C3861: 'GameDeactivate': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 126
    Error 9 error C3861: 'GamePaint': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 137
    Error 10 error C3861: 'GameEnd': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 144
    Error 11 error C3861: 'GameInitialize': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 158
    Error 12 error C3861: 'GameCycle': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 185
    Error 13 error C3861: 'GameEnd': identifier not found c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 194
    Error 14 error C2365: 'GameInitialize' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 4
    Error 15 error C2365: 'GameStart' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 14
    Error 16 error C2365: 'GameEnd' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 19
    Error 17 error C2365: 'GameActivate' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 24
    Error 18 error C2365: 'GameDeactivate' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 35
    Error 19 error C2365: 'GamePaint' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 46
    Error 20 error C2365: 'GameCycle' : redefinition; previous definition was 'formerly unknown identifier' c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Skeletor\Skeletor\Skeleton.cpp 50

    please help me on my way, thx!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: error C2065: undeclared identifier

    CODE TAGS

    Please use code tags, indentation and don't write multiple statements on a single line. Also, please post or identify the line that is generating the error(s).

    Take one of the errors, Error 2 error C2065: 'iFrameRate' : undeclared identifier c:\documents and settings\owner\my documents\visual studio 2005\projects\skeletor\skeletor\GameEngine.h 37

    Now look at this line of your code.
    void SetFrameRate(int iFameRate){m_iFrameDelay = 1000/ iFrameRate;};

    Where is iFrameRate defined?

  3. #3
    Join Date
    Jan 2009
    Posts
    14

    Re: error C2065: undeclared identifier

    i have also tried to put the functions who produce an error before the main() function, but that also results in the same errors, perhaps im forgetting a header file or something?
    Is my GameEngine.h file structure like it supposed to be?
    If i make a instance from the gameEngine class, all functions should be declared, no?
    thx in advance

  4. #4
    Join Date
    Jan 2009
    Posts
    14

    Re: error C2065: undeclared identifier

    thx for your reply, and from now on on ill use the code tags!
    so i need to declare all the items that produce c2065 error within the gameengine class?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: error C2065: undeclared identifier

    Quote Originally Posted by phisherman View Post
    thx for your reply, and from now on on ill use the code tags!
    so i need to declare all the items that produce c2065 error within the gameengine class?
    Of course. You can't use something that hasn't been defined to the compiler.

  6. #6
    Join Date
    Jan 2009
    Posts
    14

    Re: error C2065: undeclared identifier

    could you please give me one example on how and where to declare the functions that produce those errors within the gameengine class, that would help me a great deal...
    thx

  7. #7
    Join Date
    Jan 2009
    Posts
    14

    Re: error C2065: undeclared identifier

    i have made some changes to the class that resulted in 'less' errors:

    Code:
    #pragma once
    
    class GameEngine
    {
    protected:
    	//lidvariabelen
    	static GameEngine*  m_pGameEngine;
    	HINSTANCE           m_hInstance, 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, iFrameRate;
    	BOOL                m_bSleep;
    
    	//constructor/destructor
    public:
    	GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth = 640, int iHeight = 480);
    	virtual ~GameEngine();
    
    	//alg methoden
    
    	static GameEngine*   GetEngine() {return m_pGameEngine;};
    	BOOL                 Initialize(int iCmdShow);
    	LRESULT              HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);
    
    
        // changes made due to the errors!!
    
    
    
    	BOOL        GameInitialize(HINSTANCE hInstance);
            void        GameStart(HWND hWindow);
    	void        GameEnd();
    	void        GameActivate(HWND hWindow);
    	void        GameDeactivate(HWND hWindow);
    	void        GamePaint(HDC hDC);
    	void        GameCycle();
    
    
    
    
    	//accessor methodes
    	HINSTANCE   GetInstance(){return hInstance;};
    	HWND        GetWindow(){return m_hWindow;};
    	void        SetWindow(HWND hWindow){m_hWindow = hWindow;};
    	LPSTR       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 iFameRate){m_iFrameDelay = 1000/ iFrameRate;};
    	BOOL        GetSleep(){return m_bSleep;};
    	void        SetSleep(BOOL bSleep){m_bSleep = bSleep;};
        
    };
    but i still have the following errors:

    Error 1 error C2065: 'wndProc' : undeclared identifier GameEngine.h 81
    Error 2 error C3861: 'GetSytemMetrics': identifier not found GameEngine.h 97
    Error 3 error C3861: 'Createwindow': identifier not found GameEngine.h 102
    Error 4 error C3861: 'GameInitialize': identifier not found GameEngine.h 166
    Error 5 error C3861: 'GameCycle': identifier not found GameEngine.h 193
    Error 6 error C3861: 'GameEnd': identifier not found GameEngine.h 202
    Error 7 error C2365: 'GameInitialize' : redefinition; previous definition was 'formerly unknown identifier' Skeleton.cpp 4
    Error 8 error C2365: 'GameEnd' : redefinition; previous definition was 'formerly unknown identifier' Skeleton.cpp 19
    Error 9 error C2365: 'GameCycle' : redefinition; previous definition was 'formerly unknown identifier' Skeleton.cpp 50

  8. #8
    Join Date
    Sep 2011
    Posts
    2

    Re: [RESOLVED] error C2065: undeclared identifier

    I believe you are following the Sams teach yourself game programming book.

    I too am using the book at the moment and coming to the end of hour 3 but I have the same error as you:

    Wnd Proc undeclared...

    btw don't worry about the other 8 errors, they get added in at the end of hour 3, when you write the specific game code, that is ofcourse if you haven't got to that point already.

    as for the WndProc I haven't got a clue whats up with it, when I find out the problem i'll reply to you on here and let you know.

  9. #9
    Join Date
    Sep 2011
    Posts
    2

    Re: [RESOLVED] error C2065: undeclared identifier

    Hey, make sure you delare the WndProc function at the start of GameEngine.h, that sorted it for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured