Why can't I initialize WNDCLASSEX wc?

2019-08-27 20:03发布

问题:

I declare the attribute WNDCLASSEX wc in my header file like so:

private:
    HWND hWnd;
    LPDIRECT3D9 g_pD3D; // Used to create the D3DDevice
    LPDIRECT3DDEVICE9 g_pd3dDevice; // Our rendering device
    WNDCLASSEX wc;

And I want to initialize it in my Init() function, like so:

void RAT_RendererDX9::Init(RAT_WindowManager* argWMan)
{
    wMan = argWMan;

    // Register the window class
    wc  =
    {
        sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0, 0,
        GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
        "D3D Tutorial", NULL
    };
    RegisterClassEx( &wc );

       hWnd = CreateWindow( "", "abc", WS_OVERLAPPEDWINDOW, 10, 10, 20, 20,
                     NULL, NULL, wc.hInstance, NULL );

        g_pD3D = (LPDIRECT3D9)Direct3DCreate9( D3D_SDK_VERSION );

        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory( &d3dpp, sizeof( d3dpp ) );
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                              D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                              &d3dpp, &g_pd3dDevice );
}

However, I get the following errors because of it:

1>e:\rat_engine\rat_engine\rat_engine\rat_rendererdx9.cpp(18): error C2059: syntax error : '{'

1>e:\rat_engine\rat_engine\rat_engine\rat_rendererdx9.cpp(18): error C2143: syntax error : missing ';' before '{'

1>e:\rat_engine\rat_engine\rat_engine\rat_rendererdx9.cpp(19): error C3867: 'RAT_ENGINE::RAT_RendererDX9::MsgProc': function call missing argument list; use '&RAT_ENGINE::RAT_RendererDX9::MsgProc' to create a pointer to member

1>e:\rat_engine\rat_engine\rat_engine\rat_rendererdx9.cpp(22): error C2143: syntax error : missing ';' before '}'

But I practically copied that initialization from the tutorial, with the only exception that wc is initialized in the WinMain() function, instead of an Init() function.

Why isn't it working and how can I solve it?

回答1:

That syntax works for copy-initializing an aggregate, but yours is actually an assignment. The object wc gets default-constructed when you execute the constructor of your RAT_RendererDX9 class.

The assignment in your Init function is actually equivalent to this:

wc.operator = (...);

If you want to use that syntax, you could try changing your assignment into the following:

WNDCLASSEX wndClass =
{
    sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0, 0,
    GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
    "D3D Tutorial", NULL
};

wc = wndClass;