【问题标题】:Why a minimum MFC project has linking error on Visual Studio 2013?为什么最小 MFC 项目在 Visual Studio 2013 上存在链接错误?
【发布时间】:2013-11-17 11:41:29
【问题描述】:

我创建了一个 Win32 控制台应用程序来编写一个简单的 MFC 项目。

源码如下:

#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;
}

当我尝试编译时,出现以下错误:

错误 1 ​​错误 LNK2001:未解析的外部符号“受保护:虚拟结构 AFX_MSGMAP const * __thiscall MainWindow::GetMessageMap(void)const” (?GetMessageMap@MainWindow@@MBEPBUAFX_MSGMAP@@XZ) D:\Projects\MinimumMFC\MinimumMFC\最小MFC.obj 最小MFC

【问题讨论】:

  • 在使用 MFC 创建对话框应用程序时,您是否可以选择: 1. 在共享 DLL 中使用 MFC?
  • 这看起来不像一个 Win32 控制台 应用程序。考虑编辑您的问题以匹配您的问题。

标签: c++ visual-studio mfc visual-studio-2013


【解决方案1】:

我忘了在 MainWindow 声明之后声明 BEGIN_MESSAGE_MAP:

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

完整的源代码应该是:

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2016-12-20
    相关资源
    最近更新 更多