栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++多文件编译问题(1)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++多文件编译问题(1)

首先我们应该知道include命令可以理解为照搬引用的文件到该位置,然后对于每个引用了头文件的源文件(.cpp),相应的变量或者说是参数(形参和实参)都应该有相应的声明,而对于声明,结构体和函数可以多次声明,但是不能重复定义变量或者定义结构体。

而每个文件里的每个变量都应该有自己的声明与之对应,例如对于函数中的形参和实参应该在前面有相应的类型声明只有这样才能成功编译。

#define MAX 2000
#include
using namespace std;
struct person
{
	string name; //姓名
	int sex; //性别:1男 2女
	int age; //年龄
	string phone; //电话
	string addr; //住址
};
struct addressbooks
{
	struct person personarray[MAX]; //通讯录中保存的联系人数组
	int m_size; //通讯录中人员个数
};

void showmenu();
void addperson(struct addressbooks* a);
void showperson(struct addressbooks* a);
int isexist(struct addressbooks* a, string t);
void deletepersion(struct addressbooks*a);

 

#include
#include
#include"head1.h"
using namespace std;
void showmenu() {
	cout << "***************************" << endl;
	cout << "*****  1、添加联系人  *****" << endl;
	cout << "*****  2、显示联系人  *****" << endl;
	cout << "*****  3、删除联系人  *****" << endl;
	cout << "*****  4、查找联系人  *****" << endl;
	cout << "*****  5、修改联系人  *****" << endl;
	cout << "*****  6、清空联系人  *****" << endl;
	cout << "*****  0、退出通讯录  *****" << endl;
	cout << "***************************" << endl;
}
void addperson(struct addressbooks* a) {
	if (a->m_size == MAX) {
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else {
		string n;
		cout << "请输入姓名" << endl;
		cin >> a->personarray[a->m_size].name;

		cout << "请输入性别" << endl;
		cout << "1 --男" << endl << "2 --女" << endl;
		cin >> a->personarray[a->m_size].sex;

		cout << "请输入年龄" << endl;
		cin >> a->personarray[a->m_size].age;

		cout << "请输入联系方式" << endl;
		cin >> a->personarray[a->m_size].phone;

		cout << "请输入家庭地址" << endl;
		cin >> a->personarray[a->m_size].addr;

		cout << "输入成功";
		a->m_size++;
		system("pause");
		system("cls");
	}
}
void showperson(struct addressbooks* a) {
	if (a->m_size == 0) {
		cout << "通讯录里没有信息" << endl;
		return;
	}
	else {
		for (int i = 0; i < a->m_size; i++) {
			cout << "姓名:" << a->personarray[i].name << "t";
			cout << "性别:" << (a->personarray[i].sex == 1 ? "男" : "女") << "t";
			cout << "年龄:" << a->personarray[i].age << "t";
			cout << "电话:" << a->personarray[i].phone << "t";
			cout << "住址:" << a->personarray[i].addr << endl;
		}
	}
	system("pause");
	system("cls");
	return;
}
int isexist(struct addressbooks* a, string t) {
	for (int i = 0; i < a->m_size; i++) {
		if (a->personarray[i].name == t) {
			return i;
		}
	}
	return -1;
}
void deletepersion(struct addressbooks*a) {
	string t;
	cout << "请输入要查找的人的姓名" << endl;
	cin >> t;
	int ret = isexist(a, t);
	if (ret == -1) {
		cout << "查无此人" << endl;
	}
	else {
		struct person t = a->personarray[a->m_size - 1];
		a->personarray[a->m_size - 1] = a->personarray[ret];
		a->personarray[ret] = t;
		a->m_size--;
		cout << "找到此人并且删除成功" << endl;
	}
	system("pause");
	system("cls");
	return;
}

(3条消息) 【C言】结构体在多文件中的定义和声明_code_snow的博客-CSDN博客_结构体头文件声明(3条消息) 【C语言】结构体在多文件中的定义和声明_code_snow的博客-CSDN博客_结构体头文件声明

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号