急!模拟MFC的RTTI 出现连接问题
源代码如下
#include <windows.h>
#include <iostream.h>
class CObject;
struct CRuntimeClass
{
LPCTSTR m_lpszClassName;//类名
int m_nObjectSize;//对象大小
UINT m_wSchema;//类的版本号
CObject*(_stdcall *m_pfnCreateObject)();//动态创建的函数指针
CRuntimeClass *m_pBaseClass;//基类的地址
CObject *CreateObject();//动态创建的函数指针
CRuntimeClass *m_pNextClass;
BOOL IsDerivedFrom(CRuntimeClass *m_pBaseClass)const
{
const CRuntimeClass *pClassThis=this;
while(pClassThis!=NULL)
{
if(pClassThis==m_pBaseClass)
return TRUE;
pClassThis=pClassThis->m_pBaseClass;
}
return FALSE;
}
};
#define RUNTIME_CLASS(class_name) ( (CRuntimeClass*)&class_name::class##class_name)
class CObject
{
public:
static CRuntimeClass classCObject;
//成员函数
CRuntimeClass *GetRuntimeClass() const
{
return &classCObject;
}
BOOL IsKindOf(CRuntimeClass *pClass) const
{
CRuntimeClass *pClassThis=GetRuntimeClass();
return pClassThis->IsDerivedFrom(pClass);
}
};
struct CObject::classCObject =
{"CObject",sizeof(CObject),0xffff/*大小*/,NULL/*不支持动态创建*/
,NULL/*没有基类*/,NULL};
class CPerson: public CObject
{
public:
static CRuntimeClass classCPerson;
virtual CRuntimeClass* GetRuntimeClass() const
{
return RUNTIME_CLASS(CPerson);
}
};
struct CRuntimeClass CPerson::classCPerson=
{"CPerson",sizeof(CPerson),0xFFFF,NULL,&CObject::classCObject,
NULL};
void main()
{
CObject *pMyObject=new CPerson;
if( pMyObject->IsKindOf(RUNTIME_CLASS(CPerson)) )
{
cout<<"a CPerson Object!\n";
}
delete pMyObject;
}
为什么会出现
error LNK2001: unresolved external symbol "public: static struct CRuntimeClass CObject::classCObject" (?classCObject@CObject@@2UCRuntimeClass@@A)
Debug/asdfsdf.exe : fatal error LNK1120: 1 unresolved externals
如何解决?