qq_36760828 2017-05-12 02:53 采纳率: 33.3%
浏览 1095
已采纳

dev- c++ 编译错误 如图如图如图

求大神来帮我找错误,,如图。最后一张是编译时dev的报错内容

//AFX.h
#ifndef __AFX_H_
#define __AFX_H_

#include <windows.h>

class CObject;
struct CRuntimeClass
{
//  Attribute
    LPCSTR m_lpszClassName;
    int m_nObjectSize;
    UINT m_uSchema;
    CObject* (__stdcall m_pfnCreateObject)();
    CRuntimeClass* m_pBaseClass;

//  operator
    CObject* CreateObject();
    BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass);

//  Implements
    CRuntimeClass* m_pNextClass;
};

CObject* CRuntimeClass::CreateObject()
{
    if(m_pfnCreateObject == NULL)
        return NULL;
    return m_pfnCreateObject;
}

BOOL CRuntimeClass::IsDerivedFrom(const CRuntimeClass* pBaseClass)
{
    const CRuntimeClass* pClassThis = this;
    while(pClassThis != NULL)
    {
        if(pClassThis == pBaseClass)
        {
            return TRUE;
        }
        pClassThis = pClassThis->m_pBaseClass;
    }
    return FALSE;
}

#define RUNTIME_CLASS(class_name)((CRuntimeClass*)&class_name::class##class_name)

class CObject
{
public:
    virtual CRuntimeClass* GetRuntimeClass() const;
    virtual ~CObject();
public:
    BOOL IsKindOf(const CRuntimeClass* pClass) const;
public:
    static const CRuntimeClass classCObject;
};
inline CObject::~CObject(){}

static const CRuntimeClass CObject::classCObject=
{
    "CObject", sizeof(CObject), 0xffff, NULL, NULL, NULL
};

CRuntimeClass* CObject::GetRuntimeClass() const
{
    return RUNTIME_CLASS(CObject);
}

BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const
{
    CRuntimeClass* pClassThis = GetRuntimeClass();
    return pClassThis->IsDerivedFrom(pClass);
}

#endif



//main.cpp
#include <iostream>
#include "AFX.h"
using namespace std;

class CPerson : public CObject
{
public:
    virtual CRuntimeClass* GetRuntimeClass() const
    {
        return RUNTIME_CLASS(CPerson);
    }
    static CRuntimeClass classCPerson;
};

CRuntimeClass CPerson::classCPerson=
{
    "CPerson"
};

int main()
{
    CObject* pMyCObject = new CObject;

    if(pMyCObject->IsKindOf(RUNTIME_CLASS(CPerson)))
    {
        cout << "Is CPerson inherit\n";
        delete pMyCObject;
    }
    else
    {
        delete pMyCObject;
    }
    return 0;
}
  • 写回答

2条回答 默认 最新

  • stonerebel 2017-05-13 07:00
    关注

    三个地方有错:

    1、

    CObject* (__stdcall m_pfnCreateObject)();
    

    这里的意思应该是要声明一个函数指针吧,应该写成:

    CObject* (__stdcall  *m_pfnCreateObject)(void);
    

    2、

    CObject* CRuntimeClass::CreateObject()
    {
        if(m_pfnCreateObject == NULL)
            return NULL;
        return m_pfnCreateObject;
    }
    

    这个地方是要返回CObject指针,而不是函数指针,应该改为

    CObject* CRuntimeClass::CreateObject()
    {
        if(m_pfnCreateObject == NULL)
            return NULL;
        return m_pfnCreateObject();
    }
    

    3、

    static const CRuntimeClass CObject::classCObject=
    {
        "CObject", sizeof(CObject), 0xffff, NULL, NULL, NULL
    };
    

    静态常量成员在定义时不能加static,改为

    const CRuntimeClass CObject::classCObject=
    {
        "CObject", sizeof(CObject), 0xffff, NULL, NULL, NULL
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?