简体   繁体   English

为什么最小的MFC项目在Visual Studio 2013上有链接错误?

[英]Why a minimum MFC project has linking error on Visual Studio 2013?

I created a Win32 console application to write a simples MFC project. 我创建了一个Win32控制台应用程序来编写一个简单的MFC项目。

在此处输入图片说明

The source code is as follow: 源代码如下:

#include <afxwin.h>

class MyApp : public CWinApp
{
public:
    BOOL InitInstance();

    MyApp()
    {
    }
};

class MainWindow : public CFrameWnd
{
protected:
    int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnClose();
    LRESULT OnTimer(WPARAM wParam, LPARAM lParam);

    // This line is causing the error
    DECLARE_MESSAGE_MAP()
};

BOOL MyApp::InitInstance()
{
    MainWindow* mainWindow = new MainWindow();

    m_pMainWnd = mainWindow;
    mainWindow->Create(NULL, L"Main Window");
    mainWindow->ShowWindow(m_nCmdShow);

    return TRUE;
}

MyApp myApp;

int MainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    SetTimer(1, 2000, NULL);
    return 0;
}

void MainWindow::OnClose()
{
    if (MessageBox(L"Close?", L"Close", MB_YESNO | MB_ICONQUESTION) == IDYES)
    {
        KillTimer(1);
        CFrameWnd::OnClose();
    }
}

LRESULT MainWindow::OnTimer(WPARAM wParam, LPARAM lParam)
{
    MessageBeep(MB_ICONQUESTION);
    return 0;
}

When I try to compile I get the following error: 当我尝试编译时,出现以下错误:

Error 1 error LNK2001: unresolved external symbol "protected: virtual struct AFX_MSGMAP const * __thiscall MainWindow::GetMessageMap(void)const " (?GetMessageMap@MainWindow@@MBEPBUAFX_MSGMAP@@XZ) D:\\Projects\\MinimumMFC\\MinimumMFC\\MinimumMFC.obj MinimumMFC 错误1错误LNK2001:未解析的外部符号“受保护:虚拟结构AFX_MSGMAP const * __thiscall MainWindow :: GetMessageMap(void)const“(?GetMessageMap @ MainWindow @@ MBEPBUAFX_MSGMAP @@ XZ)D:\\ Projects \\ MinimumMFC \\ MinimumMFC \\ MinimumMFC.obj最低MFC

I forgot to declare the BEGIN_MESSAGE_MAP after the MainWindow declaration: 我忘了在MainWindow声明之后声明BEGIN_MESSAGE_MAP:

BEGIN_MESSAGE_MAP(MainWindow, CFrameWnd)
    ON_WM_CREATE()
    ON_WM_CLOSE()
    ON_MESSAGE(WM_TIMER, OnTimer)
END_MESSAGE_MAP()

The full source code should be: 完整的源代码应为:

#include <afxwin.h>

class MyApp : public CWinApp
{
public:
    BOOL InitInstance();

    MyApp()
    {
    }
};

class MainWindow : public CFrameWnd
{
protected:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnClose();
    afx_msg LRESULT OnTimer(WPARAM wParam, LPARAM lParam);

    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(MainWindow, CFrameWnd)
    ON_WM_CREATE()
    ON_WM_CLOSE()
    ON_MESSAGE(WM_TIMER, OnTimer)
END_MESSAGE_MAP()

BOOL MyApp::InitInstance()
{
    MainWindow* mainWindow = new MainWindow();

    m_pMainWnd = mainWindow;
    mainWindow->Create(NULL, L"Main Window");
    mainWindow->ShowWindow(m_nCmdShow);

    return TRUE;
}

MyApp myApp;

int MainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    SetTimer(1, 2000, NULL);
    return 0;
}

void MainWindow::OnClose()
{
    if (MessageBox(L"Close?", L"Close", MB_YESNO | MB_ICONQUESTION) == IDYES)
    {
        KillTimer(1);
        CFrameWnd::OnClose();
    }
}

LRESULT MainWindow::OnTimer(WPARAM wParam, LPARAM lParam)
{
    MessageBeep(MB_ICONQUESTION);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM