MSDN中在COblist::COblist的帮助里有一个实际的例子,我上机实践了一下
class CAge : public CObject
{
DECLARE_SERIAL( CAge )
private:
int m_years;
public:
CAge() { m_years = 0; }
CAge( int age ) { m_years = age; }
CAge( const CAge& a ) { m_years = a.m_years; } // Copy constructor
void Serialize( CArchive& ar);
void AssertValid() const;
const CAge& operator=( const CAge& a )
{
m_years = a.m_years; return *this;
}
BOOL operator==(CAge a)
{
return m_years == a.m_years;
}
void* operator new(size_t nSize);
void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
void operator delete(void* p);
void operator delete(void* p, LPCSTR lpszFileName, int nLine);
#ifdef _DEBUG
void Dump( CDumpContext& dc ) const
{
CObject::Dump( dc );
dc << m_years;
}
#endif
};
编译后报错:
-------------------Configuration: test - Win32 Debug--------------------
Linking...
Age.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CAge::AssertValid(void)const " (?AssertValid@CAge@@UBEXXZ)
Age.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CAge::Serialize(class CArchive &)" (?Serialize@CAge@@UAEXAAVCArchive@@@Z)
Age.obj : error LNK2001: unresolved external symbol "public: static void __cdecl CAge::operator delete(void *)" (??3CAge@@SAXPAX@Z)
Debug/test.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.test.exe - 4 error(s), 0 warning(s)我以为是因为没写函数定义所致,于是为每个函数都写了一个空的函数体。
结果错误更多,请问应该怎样实现这个CAge类?
class CAge : public CObject
{
DECLARE_SERIAL( CAge )
private:
int m_years;
public:
CAge() { m_years = 0; }
CAge( int age ) { m_years = age; }
CAge( const CAge& a ) { m_years = a.m_years; } // Copy constructor
void Serialize( CArchive& ar);
void AssertValid() const;
const CAge& operator=( const CAge& a )
{
m_years = a.m_years; return *this;
}
BOOL operator==(CAge a)
{
return m_years == a.m_years;
}
void* operator new(size_t nSize);
void* operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
void operator delete(void* p);
void operator delete(void* p, LPCSTR lpszFileName, int nLine);
#ifdef _DEBUG
void Dump( CDumpContext& dc ) const
{
CObject::Dump( dc );
dc << m_years;
}
#endif
};
编译后报错:
-------------------Configuration: test - Win32 Debug--------------------
Linking...
Age.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CAge::AssertValid(void)const " (?AssertValid@CAge@@UBEXXZ)
Age.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CAge::Serialize(class CArchive &)" (?Serialize@CAge@@UAEXAAVCArchive@@@Z)
Age.obj : error LNK2001: unresolved external symbol "public: static void __cdecl CAge::operator delete(void *)" (??3CAge@@SAXPAX@Z)
Debug/test.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.test.exe - 4 error(s), 0 warning(s)我以为是因为没写函数定义所致,于是为每个函数都写了一个空的函数体。
结果错误更多,请问应该怎样实现这个CAge类?
建意把实现的代码放在.cpp中
请问 fuxf(布衣) :
请您亲自自己试一下好吗?看看怎样才能实现自己的类。