链接过程中出现LNK2001错误:
- LNK2001:无法解析的外部符号“public: virtual struct CRuntimeClass * __thiscall CChildView::GetRuntimeClass(void)const” (?GetRuntimeClass@CChildView@@UBEPAUCRuntimeClass@@XZ)
为什么会这样?
以下是 header 中的相关代码:
class CChildView :public CDialog
{
DECLARE_DYNAMIC(CChildView)
public:
CChildView();
~CChildView();
afx_msg void OnPaint();
afx_msg void OnLevelProf();
afx_msg void OnLevelAmat();
afx_msg void OnLevelBeg();
afx_msg void OnStepC();
void new_game();
//void CloseWindow();
BOOL PreCreateWindow(CREATESTRUCT& cs);
int end_analyze();
void ii();
unsigned long calculate(int id, int x, int y);
afx_msg void OnNewGame();
//void Invalidate();
afx_msg void OnX1010();
afx_msg void OnX1919();
afx_msg void OnX3030();
afx_msg void OnX5050();
afx_msg void OnX100100();
//MessageBoxW();
void resize_window();
afx_msg void OnLButtonDown(UINT, CPoint xy);
//void GetWindowRect(RECT);
//int MessageBoxW();
void OnStepH();
void set_chеcked_menu(unsigned int old_id, unsigned int new_id);
DECLARE_MESSAGE_MAP()
};
以及.cpp文件的部分:
//IMPLEMENT_DYNAMIC(CChildView, CWnd)//!without this - doesn`t compiles. With - //runtime failure
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
.....
END_MESSAGE_MAP()
但是在我的程序执行期间(如果 Implement_dynamicaly 未注释),它在 AfxWinMain 函数中失败:
if (!pThread->InitInstance())
我的其他类没有明确定义它们,并且它们没有错误。 这是类似的想法,但这对我没有帮助。 MFC dlg class link errors for MyClass::GetMessageMap() and MyClass::GetRuntimeClass (MSVC 2008)
最佳答案
您注释掉了 IMPLMENT_DYNAMIC(CChildView, CWnd) 行。
您需要注释掉 CChildView 类中的 DECLARE_DYNAMIC() 宏,或者取消注释 IMPLMENT_DYNAMIC——它们与您的类的 CRuntimeClass 有关。另外,如果取消注释 IMPLMENT_DYNAMIC,则应确保宏中的基类与您派生的类匹配。 IOW,它应该说 CDialog 而不是 CWnd。此外,您的 BEGIN_MESSAGE_MAP() 也遇到同样的问题。
关于c++ - 如何消除mfc中的链接错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683388/