互相包含的类如何声明
有两个类CParent,CSon.CParent 有一个CSon的成员、一个返回自己的朋友数量的成员函数、一个返回儿子同学数量的成员函数;CSon有一个CParent的成员、一个返回自己的同学数量的成员函数,一个返回父亲朋友数量的成员函数。
编译时在两个“****”处出错,错误信息
e:\test\son.h(18) : error C2079: 'm_parent' uses undefined class 'CParent'
e:\test\parent.h(19) : error C2079: 'm_son' uses undefined class 'CSon'
类使用类向导生成。
头文件parent.h
#if !defined(AFX_PARENT_H__DBCE2E48_4D66_11D4_8FAF_0050BA0694EB__INCLUDED_)
#define AFX_PARENT_H__DBCE2E48_4D66_11D4_8FAF_0050BA0694EB__INCLUDED_
//#include "Son.h" // Added by ClassView
class CSon;
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CParent
{
public:
int FriendCount();
int ClassmateCountOfSon();
CSon m_son;
CParent();
virtual ~CParent();
};
#endif // !defined(AFX_PARENT_H__DBCE2E48_4D66_11D4_8FAF_0050BA0694EB__INCLUDED_)
实现文件Parent.cpp:
// Parent.cpp: implementation of the CParent class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Test.h"
#include "Son.h"
#include "Parent.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//written by lgb
//#include "Son.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CParent::CParent()
{
}
CParent::~CParent()
{
}
int CParent::ClassmateCountOfSon()
{
return m_son.ClassmateCount();
}
int CParent::FriendCount()
{
return 10;
}
头文件Son.h
// Son.h: interface for the CSon class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SON_H__DBCE2E49_4D66_11D4_8FAF_0050BA0694EB__INCLUDED_)
#define AFX_SON_H__DBCE2E49_4D66_11D4_8FAF_0050BA0694EB__INCLUDED_
//#include "Parent.h" // Added by ClassView
class CParent;
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSon
{
public:
int FriendCountOfParent();
CParent m_parent;
int ClassmateCount();
CSon();
virtual ~CSon();
};
#endif // !defined(AFX_SON_H__DBCE2E49_4D66_11D4_8FAF_0050BA0694EB__INCLUDED_)
实现文件Son.cpp
// Son.cpp: implementation of the CSon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Test.h"
#include "Parent.h"
#include "Son.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//written by lgb
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSon::CSon()
{
}
CSon::~CSon()
{
}
int CSon::ClassmateCount()
{
return 5;
}
int CSon::FriendCountOfParent()
{
return m_parent.FriendCount();
}
请各位高手指教!!!不胜感激!!!