PuK
PuK

Reputation: 315

C2065 undeclared identifier

I have got a problem but I don't know why. I'm fallowing this to learn DirectX. I used this to configure the solution.

This is my code:

Header:

#include <d3d11.h>
#include <d3dx11.h>
#include <d3d10.h>
#include <d3dx10.h>
#include <dxgi.h>
#include <Windows.h>
#include <windowsx.h>

#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")

    class HelloWorld
    {
    private: 
        IDXGISwapChain* swapchain;             
        ID3D11Device* dev;                     
        ID3D11DeviceContext* devcon; 

        LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

    public:
        void InitD3D(HWND hWnd);    
        void CleanD3D(void);  
        int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
    };
}

and cpp:

#include "HelloWorld.h"

void InitD3D(HWND hWnd)
{
    DXGI_SWAP_CHAIN_DESC scd;

    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

    scd.BufferCount = 1;
    scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    scd.OutputWindow = hWnd;
    scd.SampleDesc.Count = 4;
    scd.Windowed = TRUE;

    D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE,
        NULL, NULL, NULL, NULL, 
        D3D11_SDK_VERSION, &scd, &swapchain, &dev, NULL, &devcon);
}

void CleanD3D(void)
{
    swapchain->Release();
    dev->Release();
    devcon->Release();
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_DESTROY: 
        {
            PostQuitMessage(0);
            return 0;
        }break;
    }

    return DefWindowProc(hWnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = "WindowClass1";

    RegisterClassEx(&wc);

    RECT wr = {0, 0, 400, 400};
    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);

    hWnd = CreateWindowEx(NULL,
        "WindowClass1",
        "First windowed program",
        WS_OVERLAPPEDWINDOW,
        300,
        300,
        wr.right - wr.left,
        wr.bottom - wr.top,
        NULL,
        NULL,
        hInstance,
        NULL);

    ShowWindow(hWnd, nCmdShow);

    MSG msg = {0};

    while(true)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);

            if(msg.message == WM_QUIT)
            {
                break;
            }
        }
        else
        {

        }
    }

    return msg.wParam;
}

But during compilation I have got a C2065 error on : swapchain, dev and devcon.

I have tried several thing but nothing worked.

If somebody could help my, it will be appreciate !

PuK

Upvotes: 0

Views: 2852

Answers (2)

holtavolt
holtavolt

Reputation: 4468

It would appear you've mixed a C++ "HelloWorld" class with C-style functions.

As a first step, I'd recommend you simply remove the header, and make the currently private variables static to the .cpp.

Once you have that working, you can wrap it up in a class. To convert your functions to methods, you'll need to name them as such in the .cpp, e.g.

void HelloWorld::InitD3D(HWND hWnd)

Upvotes: 1

WhozCraig
WhozCraig

Reputation: 66244

Consider adding a class-qualifier to your Init3D function.

void InitD3D(HWND hWnd)
{
   ...

Should be:

void HelloWorld::InitD3D(HWND hWnd)
{
    ...

Note: similar problem exists for other functions, and you'll find out soon enough your WndProc will need some special handling to wire up to your C++ object.

Upvotes: 1

Related Questions