首页 > 解决方案 > Microsft Visual Studio C++ 2019 编译错误与 IMPLEMENT_DYNCREATE

问题描述

我正在开发一个需要运行多任务的程序,以便我可以运行一个计时器。

这是包含文件(MyRunTimers.h):

#pragma once
#include <afxwin.h>

class MyRunTimers :
    public CObject
{
    int     m_nNum;
    bool m_bDone = FALSE;
    CObject* m_pOwner = NULL;
    bool m_bAutoDelete = FALSE;

    void MyTimerThread();

public:
     DECLARE_DYNAMIC(MyRunTimers)

protected:
    MyRunTimers::MyRunTimers();

    MyRunTimers::~MyRunTimers();

};

这是 cpp 文件(MyRunTimers.cpp):

#include "pch.h"
#include "MyRunTimers.h"

IMPLEMENT_DYNCREATE(MyRunTimers, CObject)

MyRunTimers::MyRunTimers()
{
    m_bDone = FALSE;
    m_pOwner = NULL;
    m_bAutoDelete = FALSE;
}

MyRunTimers::~MyRunTimers()
{
}

当我尝试编译时,IMPLEMENT_DYNCREATE它以红色突出显示,我得到:

`E0135 - class "MyRunTimers" has no member "CreateObject"

标签: c++windowsmfcvisual-studio-2019

解决方案


If you have DECLARE_DYNAMIC(MyClass) in the class declaration (header file), then you need IMPLEMENT_DYNAMIC(MyClass, BaseClass) in the definition file.

Alternatively, if you actually need IMPLEMENT_DYNCREATE(), then you should have DECLARE_DYNCREATE() in the class definition.

For a discussion about the differences, see here: Why should i use DECLARE_DYNAMIC instead of DECLARE_DYNCREATE?.


推荐阅读