在编写模板类的一个成员函数时,有如下一个函数:
template <class Owned>
void COwner<Owned>::KillChild()
{
if ( m_pOwned )
{
delete m_pOwned;
m_pOwned = 0;
}
}当class Owned实例化为class B后,编译时报警告:
warning C4150: 删除指向不完整“B”类型的指针;没有调用析构函数
请问高手如何能解决此问题?完整代码文件和编译警告如下://///////////////////////////////////////////////////////////////////
//Container.h
/////////////////////////////////////////////////////////////////////
//class COwner//define
template <class Owned>
class COwner
{
public:
COwner(void);
virtual ~COwner(void);private:
Owned* m_pOwned;
public:
Owned* GetChild() const;
bool SpecifyChild(Owned* & pChild);
void KillChild();
bool HasChild() const;
};//implement
template <class Owned>
COwner<Owned>::COwner(void)
:m_pOwned(0)
{
}template <class Owned>
COwner<Owned>::~COwner(void)
{
if ( HasChild() )
{
KillChild();
}
}template <class Owned>
Owned* COwner<Owned>::GetChild() const
{
return m_pOwned;
}template <class Owned>
bool COwner<Owned>::SpecifyChild(Owned* & pChild)
{
ASSERT(pChild); if ( !m_pOwned )
{
m_pOwned = pChild;
return true;
} if ( m_pOwned && m_pOwned == pChild )
{
return true;
} return false;
}template <class Owned>
void COwner<Owned>::KillChild()
{
if ( m_pOwned )
{
delete m_pOwned;
m_pOwned = 0;
}
}template <class Owned>
bool COwner<Owned>::HasChild() const
{
return m_pOwned != 0;
}
//class COwner
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//class COwned
//define
template <class Owner>
class COwned
{
public:
COwned(Owner* const& pOwner);
virtual ~COwned(void);private:
Owner* m_pOwner;
public:
void SetOwner(Owner* const& pOwner);
bool HasOwner() const;
Owner* GetOwner() const;
};//implement
template <class Owner>
COwned<Owner>::COwned(Owner* const& pOwner)
:m_pOwner(pOwner)
{
}template <class Owner>
COwned<Owner>::~COwned()
{
HasOwner();
}template <class Owner>
void COwned<Owner>::SetOwner(Owner* const& pOwner)
{
ASSERT(pOwner); // if change from a Owner to another.
if ( m_pOwner )
{
// if Owner is COwner<COwned>
if ( typeid(*m_pOwner) == typeid(COwner<COwned>) )
{
((COwner<COwned>*)m_pOwner)->DeleteChild(this);
}
m_pOwner = 0;
} m_pOwner = pOwner;
}template <class Owner>
bool COwned<Owner>::HasOwner() const
{
return m_pOwner != 0;
}template <class Owner>
Owner* COwned<Owner>::GetOwner() const
{
return m_pOwner;
}
//class COwned
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Container.cpp
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "container.h"/////////////////////////////////////////////////////////////////////
//A.h
/////////////////////////////////////////////////////////////////////
#pragma once
#include "container.h"class B;class A :
public COwner<B>
{
public:
A();
virtual ~A(void);
};
/////////////////////////////////////////////////////////////////////
//A.cpp
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "A.h"A::A()
{
}A::~A(void)
{
//childs' destruct done by super class COwner
}/////////////////////////////////////////////////////////////////////
//B.h
/////////////////////////////////////////////////////////////////////
#pragma once
#include "container.h"class A;class B :
public COwned<A>
{
public:
B(A* const& pOwner);
virtual ~B(void);
};/////////////////////////////////////////////////////////////////////
//B.cpp
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "B.h"
#include "A.h"B::B(A* const& pOwner)
:COwned<A>(pOwner)
{
}B::~B(void)
{
}/////////////////////////////////////////////////////////////////////
编译警告:
/////////////////////////////////////////////////////////////////////
d:\Project\Container.h(201) : warning C4150: 删除指向不完整“B”类型的指针;没有调用析构函数
d:\Project\A.h(6) : 参见“B”的声明
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xmemory(151) : 编译类模板成员函数“void COwner<Owned>::KillChild(void)”时
with
[
Owned=B
]
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xmemory(136) : 编译类模板成员函数“COwner<Owned>::~COwner(void)”时
with
[
Owned=B
]
d:\Project\KStudio\src\ui\DataObj\Kdc\AreaConstraint\A.h(12) : 参见对正在编译的类模板实例化“COwner<Owned>”的引用
with
[
Owned=B
]
template <class Owned>
void COwner<Owned>::KillChild()
{
if ( m_pOwned )
{
delete m_pOwned;
m_pOwned = 0;
}
}当class Owned实例化为class B后,编译时报警告:
warning C4150: 删除指向不完整“B”类型的指针;没有调用析构函数
请问高手如何能解决此问题?完整代码文件和编译警告如下://///////////////////////////////////////////////////////////////////
//Container.h
/////////////////////////////////////////////////////////////////////
//class COwner//define
template <class Owned>
class COwner
{
public:
COwner(void);
virtual ~COwner(void);private:
Owned* m_pOwned;
public:
Owned* GetChild() const;
bool SpecifyChild(Owned* & pChild);
void KillChild();
bool HasChild() const;
};//implement
template <class Owned>
COwner<Owned>::COwner(void)
:m_pOwned(0)
{
}template <class Owned>
COwner<Owned>::~COwner(void)
{
if ( HasChild() )
{
KillChild();
}
}template <class Owned>
Owned* COwner<Owned>::GetChild() const
{
return m_pOwned;
}template <class Owned>
bool COwner<Owned>::SpecifyChild(Owned* & pChild)
{
ASSERT(pChild); if ( !m_pOwned )
{
m_pOwned = pChild;
return true;
} if ( m_pOwned && m_pOwned == pChild )
{
return true;
} return false;
}template <class Owned>
void COwner<Owned>::KillChild()
{
if ( m_pOwned )
{
delete m_pOwned;
m_pOwned = 0;
}
}template <class Owned>
bool COwner<Owned>::HasChild() const
{
return m_pOwned != 0;
}
//class COwner
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//class COwned
//define
template <class Owner>
class COwned
{
public:
COwned(Owner* const& pOwner);
virtual ~COwned(void);private:
Owner* m_pOwner;
public:
void SetOwner(Owner* const& pOwner);
bool HasOwner() const;
Owner* GetOwner() const;
};//implement
template <class Owner>
COwned<Owner>::COwned(Owner* const& pOwner)
:m_pOwner(pOwner)
{
}template <class Owner>
COwned<Owner>::~COwned()
{
HasOwner();
}template <class Owner>
void COwned<Owner>::SetOwner(Owner* const& pOwner)
{
ASSERT(pOwner); // if change from a Owner to another.
if ( m_pOwner )
{
// if Owner is COwner<COwned>
if ( typeid(*m_pOwner) == typeid(COwner<COwned>) )
{
((COwner<COwned>*)m_pOwner)->DeleteChild(this);
}
m_pOwner = 0;
} m_pOwner = pOwner;
}template <class Owner>
bool COwned<Owner>::HasOwner() const
{
return m_pOwner != 0;
}template <class Owner>
Owner* COwned<Owner>::GetOwner() const
{
return m_pOwner;
}
//class COwned
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Container.cpp
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "container.h"/////////////////////////////////////////////////////////////////////
//A.h
/////////////////////////////////////////////////////////////////////
#pragma once
#include "container.h"class B;class A :
public COwner<B>
{
public:
A();
virtual ~A(void);
};
/////////////////////////////////////////////////////////////////////
//A.cpp
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "A.h"A::A()
{
}A::~A(void)
{
//childs' destruct done by super class COwner
}/////////////////////////////////////////////////////////////////////
//B.h
/////////////////////////////////////////////////////////////////////
#pragma once
#include "container.h"class A;class B :
public COwned<A>
{
public:
B(A* const& pOwner);
virtual ~B(void);
};/////////////////////////////////////////////////////////////////////
//B.cpp
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "B.h"
#include "A.h"B::B(A* const& pOwner)
:COwned<A>(pOwner)
{
}B::~B(void)
{
}/////////////////////////////////////////////////////////////////////
编译警告:
/////////////////////////////////////////////////////////////////////
d:\Project\Container.h(201) : warning C4150: 删除指向不完整“B”类型的指针;没有调用析构函数
d:\Project\A.h(6) : 参见“B”的声明
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xmemory(151) : 编译类模板成员函数“void COwner<Owned>::KillChild(void)”时
with
[
Owned=B
]
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xmemory(136) : 编译类模板成员函数“COwner<Owned>::~COwner(void)”时
with
[
Owned=B
]
d:\Project\KStudio\src\ui\DataObj\Kdc\AreaConstraint\A.h(12) : 参见对正在编译的类模板实例化“COwner<Owned>”的引用
with
[
Owned=B
]
比如UINT B::Release()
{
if(m_nRefCount>1)
{
m_nRefCount--;return m_nRefCount;
}
else if(m_nRefCount==1)
{
m_nRefCount--;
delete this;
return 0;
}
printf("Error: release when reference count=0\n");
return 0;
}