【问题标题】:LNK2019 error; unresolved external symbol | C++LNK2019 错误;未解析的外部符号 | C++
【发布时间】:2018-07-04 07:26:41
【问题描述】:

这是我为学校做的一个项目。我相信该程序应该可以运行,但我有一个我无法弄清楚的链接器错误。

错误:严重代码描述项目文件行抑制状态 函数 _wWinMain@16 Direct XC 中引用的错误 LNK2019 未解析的外部符号“long stdcall WndProc(struct HWND *,unsigned int,unsigned int,long)”(?WndProc@@YGJPAUHWND__@@IIJ@Z) :\Visual Studio Programs\Direct X\Direct X\main.obj 1

Main.cpp 文件

#include <Windows.h>
#include <memory>
#include "BlankDemo.h"

LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
    WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
    LPWSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER(prevInstance);
    UNREFERENCED_PARAMETER(cmdLine);

    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof(WNDCLASS);
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";

    if (!RegisterClassEx(&wndClass))
        return -1;

    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect(&rc, WS_EX_OVERLAPPEDWINDOW, FALSE);

     HWND hwnd = CreateWindowA("DX11BookWindowClass", "BlankWin32Window",
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right -      rc.left,
        rc.bottom - rc.top, NULL, NULL, hInstance, NULL);

    if (!hwnd)
        return -1;

    ShowWindow(hwnd, cmdShow);

    std::auto_ptr<Dx11DemoBase> demo(new BlankDemo());

    // Demo Initialize
    bool result = demo->Initialize(hInstance, hwnd);

    // Error reporting if there is an issue
    if (result == false)
        return -1;

    MSG msg = { 0 };

    while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        else
        {
            // Update and Draw
            demo->Update(0.0f);
            demo->Render();
        }
    }
    // Demo Shutdown
    demo->Shutdown();

    return static_cast<int>(msg.wParam);
}

 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM 1Param)
{
    PAINTSTRUCT paintStruct;
    HDC hDC;

    switch (message)
    {
    case WM_PAINT:
        hDC = BeginPaint(hwnd, &paintStruct);
        EndPaint(hwnd, &paintStruct);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    defualt:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
     return 0;
}

// implementation of the BlankDemo class
BlankDemo::BlankDemo()
{

}

BlankDemo::~BlankDemo()
{

}

bool BlankDemo::LoadContent()
{
    return true;
}

void BlankDemo::UnloadContent()
{

}

void BlankDemo::Update(float dt)
{

}

void BlankDemo::Render()
{
    if (d3dContext_ == 0)
        return;

    float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
    d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);

    swapChain_->Present(0, 0);
 }

BlankDemo.h 文件:

#pragma once
#ifndef _BLANK_DEMO_H_
#define _BLANK_DEMO_H_
#include "Dx11DemoBase.h"

class BlankDemo : public Dx11DemoBase
{
public:
    BlankDemo();
    virtual ~BlankDemo();

    bool LoadContent();
    void UnloadContent();

    void Update(float dt);
    void Render();
};

#endif // !_BLANK_DEMO_H_

最后是 Dx11DemoBase.h 文件:

#pragma once
#ifndef _DEMO_BASE_H_
#define _DEMO_BASE_H_

#include <d3d11.h>
#include <D3DX11.h>
#include <DxErr.h>

class Dx11DemoBase
{
public:
    Dx11DemoBase();
    virtual ~Dx11DemoBase();

    bool Initialize(HINSTANCE hInstance, HWND hwnd);
    void Shutdown();

    virtual bool LoadContent();
    virtual void UnloadContent();

    virtual void Update(float dt) = 0;
    virtual void Render() = 0;

protected:

    HINSTANCE hInstance_;

    HWND hwnd_;

    D3D_DRIVER_TYPE driverType_;
    D3D_FEATURE_LEVEL featureLevel_;

    ID3D11Device* d3dDevice_;
    ID3D11DeviceContext* d3dContext_;
    IDXGISwapChain* swapChain_;
    ID3D11RenderTargetView* backBufferTarget_;
};

#endif // !_DEMO_BASE_H_

【问题讨论】:

  • 我得到了编译错误而不是链接错误。在 WndProc 变量名“1Param”的定义中定义错误。它应该是“lParam”
  • 它应该已经是一个“lParam”了。除非我没有正确复制代码,否则它在我的代码中是“lParam”。
  • WndProc 定义中的1Param 替换为lParam。第一个的前缀是1 (One) 而不是l (small L)

标签: c++ visual-studio-2017 directx lnk2019


【解决方案1】:

这是你对WndProc的定义:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM 1Param)

WndProc 定义中的1Param 替换为lParam1Param的前缀是1 (One)而不是l (small L)

此外,由于您使用 Direct3D API 渲染到客户区域,因此您不需要处理 WM_PAINT 消息。 Direct3D 绘制 API 调用会将您的管道数据渲染到客户区

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2012-08-31
    相关资源
    最近更新 更多