笔记:解决文件互相包含问题的小方法
2007-07-16 09:55
435 查看
两个头文件互相包含,具体如下:
//------a.h ------
#ifndef _A_
#define _A_
#include "B.h"
typedef struct _Node
{
B* pB;
_Node* pNext;
}Node, *pNode;
class A
{
public:
A();
~A();
private:
B m_B; //此处有问题
Node *m_pHead;
};
#endif
//------b.h ------
#ifndef _B_
#define _B_
#include "A.h"
class B
{
public:
B(A* pA);
~B();
private:
A m_A; //此处有问题
};
#endif
//------main.cpp ------
#include "A.h"
#include "B.h"
int main()
{
A A();
B B(&A);
return 0;
}
编译时有错误,具体错误记不清了,就是一些文件包含重定义或者什么变量没有定义的错误,现在经过修改后编译通过:
//------a.h ------
#ifndef _A_
#define _A_
//#include "B.h" //修改处1
class B; //修改处2
typedef struct _Node
{
B* pB;
_Node* pNext;
}Node, *pNode;
class A
{
public:
A();
~A();
private:
B* m_B; //修改处3,记得一定要用指针,至于为什么这样现在还没想清楚
Node *m_pHead;
};
#endif
//------b.h ------
#ifndef _B_
#define _B_
//#include "A.h" //修改处4
class A; //修改处5
class B
{
public:
B();
~B();
private:
A* m_pA; //修改处6,关键还是要用指针
};
#endif
//------main.cpp ------
#include "A.h"
#include "B.h"
#include <afx.h>
int main()
{
A A();
B B(&A);
return 0;
}
//------a.h ------
#ifndef _A_
#define _A_
#include "B.h"
typedef struct _Node
{
B* pB;
_Node* pNext;
}Node, *pNode;
class A
{
public:
A();
~A();
private:
B m_B; //此处有问题
Node *m_pHead;
};
#endif
//------b.h ------
#ifndef _B_
#define _B_
#include "A.h"
class B
{
public:
B(A* pA);
~B();
private:
A m_A; //此处有问题
};
#endif
//------main.cpp ------
#include "A.h"
#include "B.h"
int main()
{
A A();
B B(&A);
return 0;
}
编译时有错误,具体错误记不清了,就是一些文件包含重定义或者什么变量没有定义的错误,现在经过修改后编译通过:
//------a.h ------
#ifndef _A_
#define _A_
//#include "B.h" //修改处1
class B; //修改处2
typedef struct _Node
{
B* pB;
_Node* pNext;
}Node, *pNode;
class A
{
public:
A();
~A();
private:
B* m_B; //修改处3,记得一定要用指针,至于为什么这样现在还没想清楚
Node *m_pHead;
};
#endif
//------b.h ------
#ifndef _B_
#define _B_
//#include "A.h" //修改处4
class A; //修改处5
class B
{
public:
B();
~B();
private:
A* m_pA; //修改处6,关键还是要用指针
};
#endif
//------main.cpp ------
#include "A.h"
#include "B.h"
#include <afx.h>
int main()
{
A A();
B B(&A);
return 0;
}
相关文章推荐
- 解决C/C++ 头文件相互包含 问题的方法
- 解决头文件相互包含问题的方法----超前引用- -
- 解决头文件相互包含问题的方法
- MFC之头文件相互包含问题及解决方法 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
- 表单包含file上传文件提交,使用ajaxSubmint或者ajaxForm 遇到的问题以及解决方法
- 解决头文件相互包含问题的方法
- 解决头文件相互包含问题的方法 (转)
- 回调函数——一个化解C++互相包含头文件问题的方法
- 解决头文件相互包含问题的方法
- MySQL笔记-最简单的方法来解决找不到mysqld.sock文件的问题
- 解决C/C++ 头文件相互包含 问题的方法
- 解决头文件相互包含问题的方法
- 解决头文件相互包含问题的方法----超前引用
- MFC之头文件相互包含问题及解决方法
- 使用预声明解决结构体互相引用以及头文件内容互相包含的问题[转]
- 解决头文件相互包含问题的方法
- VC++开发中解决头文件相互包含问题的方法
- VC++开发中解决头文件相互包含问题的方法
- VC++开发中完美解决头文件相互包含问题的方法解析
- 解决头文件相互包含问题的方法