C++防止头文件重复引用

1、#pragma once

#pragma once
#include<iostream>
using namespace std;
class Student
{
public:
	Student()
	{
		cout << "I am a student." << endl;
	}
};
#pragma once
//#include<iostream>
#include "student.h"
//using namespace std;
class School
{
public:
	School()
	{
		cout << "This is a school." << endl;
		Student stu;
	}
};
#include "student.h"
#include "school.h"
int main()
{
	Student a;
	School b;
	return 0;
}
I am a student.
This is a school.
I am a student.

2、#ifndef xx #define xx xxxx #endif

#include<iostream>
using namespace std;
#ifndef _STUDENT_H
#define _STUDENT_H
class Student
{
public:
	Student()
	{
		cout << "I am a student." << endl;
	}
};
#endif // !_STUDENT_H

//#include<iostream>
#include "student.h"
//using namespace std;
#ifndef _SCHOOL_H
#define _SCHOOL_H
class School
{
public:
	School()
	{
		cout << "This is a school." << endl;
		Student stu;
	}
};
#endif // !_SCHOOL_H
#include "student.h"
#include "school.h"
int main()
{
	Student a;
	School b;
	return 0;
}
I am a student.
This is a school.
I am a student.

·引用
**平时都是在别人的项目代码基础上加自己的内容,其实他们的头文件也有,没注意到,可惜。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值