鱼C论坛

 找回密码
 立即注册
查看: 1208|回复: 5

[已解决]函数传参问题第118行

[复制链接]
发表于 2022-2-6 19:30:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 孤世星辰 于 2022-2-6 19:32 编辑

118行book传参会报错
22.png
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #define MAX 1000

  5. struct person
  6. {
  7.         string name;
  8.         int sex;//1男   2女
  9.         int age;
  10.         string phone;
  11.         string add;
  12. };
  13. struct addbook
  14. {
  15.         struct person personArray[MAX];
  16.         int size;
  17. };
  18. void showMenu()
  19. {
  20.         cout << "*************************" << endl;
  21.         cout << "***** 1、添加联系人 *****" << endl;
  22.         cout << "***** 2、显示联系人 *****" << endl;
  23.         cout << "***** 3、删除联系人 *****" << endl;
  24.         cout << "***** 4、查找联系人 *****" << endl;
  25.         cout << "***** 5、修改联系人 *****" << endl;
  26.         cout << "***** 6、清空通讯录 *****" << endl;
  27.         cout << "***** 0、退出通讯录 *****" << endl;
  28.         cout << "*************************" << endl;

  29. }

  30. void addPerson(struct addbook *book)
  31. {
  32.         if (book->size == MAX)
  33.         {
  34.                 cout << "联系人已满" << endl;
  35.                 return;

  36.         }
  37.         else
  38.         {
  39.                 string name;
  40.                 cout << "请输入联系人姓名" << endl;
  41.                 cin >> name;
  42.                 book->personArray[book->size].name = name;

  43.                 int sex;
  44.                 cout << "请输入联系人性别    1、男    2、女" << endl;
  45.                 while (true)
  46.                 {
  47.                         cin >> sex;
  48.                         if (sex == 1 || sex == 2)
  49.                         {
  50.                                 book->personArray[book->size].sex = sex;
  51.                                 break;
  52.                         }
  53.                         cout << "输入有误请重新输入" << endl;
  54.                 }                               

  55.                 int age;
  56.                 cout << "请输入联系人年龄" << endl;
  57.                 cin >> age;
  58.                 book->personArray[book->size].age = age;

  59.                 string phone;
  60.                 cout << "请输入联系人电话" << endl;
  61.                 cin >> phone;
  62.                 book->personArray[book->size].phone = phone;

  63.                 string add;
  64.                 cout << "请输入联系人住址" << endl;
  65.                 cin >> add;
  66.                 book->personArray[book->size].add = add;

  67.                 book->size++;
  68.                 cout << "添加成功" << endl;
  69.                 system("pause");
  70.                 system("cls");
  71.         }

  72. }
  73. void shouPerson(struct addbook book)
  74. {
  75.         if (book.size == 0)
  76.         {
  77.                 cout << " 当前记录为空" << endl;
  78.         }
  79.         for (int i = 0; i < book.size; i++)
  80.         {
  81.                 cout << "姓名:" << book.personArray[i].name << "\t";
  82.                 cout << "性别:" << (book.personArray[i].sex == 1 ? "男":"女") << "\t";
  83.                 cout << "年龄:" << book.personArray[i].age << "\t";
  84.                 cout << "电话:" << book.personArray[i].phone << "\t";
  85.                 cout << "住址:" << book.personArray[i].add << endl;
  86.         };
  87.         system("pause");
  88.         system("cls");
  89. }
  90. int isExist(struct addbook book,string name)
  91. {
  92.         for (int i = 0; i < book.size; i++)
  93.         {
  94.                 if (book.personArray[i].name == name)
  95.                 {
  96.                         return i;
  97.                 }
  98.                 return -1;
  99.         }
  100. }
  101. void deletePerson(struct addbook *book)
  102. {
  103.         cout << "请输入需要删除的联系人" << endl;

  104.         string name;
  105.         cin >> name;

  106.         int ret = isExist(book, name);                                               //这个book传参为啥会报错
  107. }
  108. int main()
  109. {
  110.         struct addbook book;
  111.         book.size = 0;
  112.         int select = 0;
  113.         while (1)
  114.         {
  115.                 showMenu();
  116.                
  117.                 cin >> select;
  118.                 switch (select)
  119.                 {
  120.                 case 1:addPerson(&book);//利用地址传递修改
  121.                         break;
  122.                 case 2: shouPerson(book);
  123.                         break;

  124.                 case 3:deletePerson(&book);
  125.                         break;

  126.                 case 4:
  127.                         break;

  128.                 case 5:
  129.                         break;

  130.                 case 6:
  131.                         break;

  132.                 case 0:
  133.                         cout << "欢迎下次使用" << endl;
  134.                         system("pause");
  135.                         return 0;
  136.                         break;
  137.                 default:
  138.                         break;
  139.                 }

  140.         }
  141.        

  142.         system("pause");
  143.         return 0;
  144. }
复制代码
最佳答案
2022-2-6 20:03:19
  1. void deletePerson(struct addbook *book)  // 注意这里的 book 是 addbook *
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-6 19:37:19 | 显示全部楼层
改成
  1. int ret = isExist(*book, name);
复制代码

试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-6 19:55:05 | 显示全部楼层

为什么要传这个,可以解释一下嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-6 20:00:02 | 显示全部楼层
因为你传的book是指针(addbook *),而参数不是指针(addbook),所以你得把book变成*book
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-6 20:03:19 | 显示全部楼层    本楼为最佳答案   
  1. void deletePerson(struct addbook *book)  // 注意这里的 book 是 addbook *
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-6 21:49:25 | 显示全部楼层
  1. int isExist(struct addbook book,string name) //这里book不是指针变量
  2. {
  3.         for (int i = 0; i < book.size; i++)
  4.         {
  5.                 if (book.personArray[i].name == name)
  6.                 {
  7.                         return i;
  8.                 }
  9.                 return -1;
  10.         }
  11. }
  12. void deletePerson(struct addbook *book)//这里book是指针变量
  13. {
  14.         cout << "请输入需要删除的联系人" << endl;

  15.         string name;
  16.         cin >> name;

  17.         int ret = isExist(book, name);                                               //这里的形参是一般变量,但book确是指针变量,应该用 *book 替代 book
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 02:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表