c++ #include 类互相包含问题

A.h文件

#ifndef _A_H_
#define _A_H_

#include"B.h"
static int count=0;

class A
{
public:
void fun1();
};

#endif


B.h文件

#ifndef _B_H_
#define _B_H_

class A; //注意:这里是C++类的前向声明,没有用include“A.h”把对方加进来是考虑到了没有必要,因为最终两个类是要编译到一块

class B
{
public:
void fun2();
};
#endif


A.cpp文件

#include "stdafx.h"
#include "A.h"
#include <iostream>

using std::cout;
using std::endl;

void A::fun1()
{
cout<<"a"<<endl<<count++<<endl;
if(count==1000)
{
cout<<"太多了,停不下来了";
getchar();
exit(0);
}
B b;
b.fun2();
}


B.cpp文件

#include "stdafx.h"
#include "A.h" //注意:这个地方没用B.h是考虑到了编译连接的顺序
#include <iostream>

using std::cout;
using std::endl;

void B::fun2()
{
cout<<"b"<<endl<<count++<<endl;
if(count==1000)
{
cout<<"太多了,停不下来了";
getchar();
exit(0);
}
A a;
a.fun1();
}


main程序文件

#include "stdafx.h"
#include<iostream>
#include"A.h" //注意:这个地方没有include“B.h”但是下面用的了B类,说明B类头文件肯定在A.h中有include。

using std::cout;
using std::endl;

void main()
{
A a;
B b;
a.fun1();
b.fun2();
getchar();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值