鱼C论坛

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

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

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

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

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

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

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

struct person
{
        string name;
        int sex;//1男   2女
        int age;
        string phone;
        string add;
};
struct addbook
{
        struct person personArray[MAX];
        int size;
};
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 addbook *book)
{
        if (book->size == MAX)
        {
                cout << "联系人已满" << endl;
                return;

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

                int sex;
                cout << "请输入联系人性别    1、男    2、女" << endl;
                while (true)
                {
                        cin >> sex;
                        if (sex == 1 || sex == 2)
                        {
                                book->personArray[book->size].sex = sex;
                                break;
                        }
                        cout << "输入有误请重新输入" << endl;
                }                                

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

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

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

                book->size++;
                cout << "添加成功" << endl;
                system("pause");
                system("cls");
        }

}
void shouPerson(struct addbook book)
{
        if (book.size == 0)
        {
                cout << " 当前记录为空" << endl;
        }
        for (int i = 0; i < book.size; i++)
        {
                cout << "姓名:" << book.personArray[i].name << "\t";
                cout << "性别:" << (book.personArray[i].sex == 1 ? "男":"女") << "\t";
                cout << "年龄:" << book.personArray[i].age << "\t";
                cout << "电话:" << book.personArray[i].phone << "\t";
                cout << "住址:" << book.personArray[i].add << endl;
        };
        system("pause");
        system("cls");
}
int isExist(struct addbook book,string name)
{
        for (int i = 0; i < book.size; i++)
        {
                if (book.personArray[i].name == name)
                {
                        return i;
                }
                return -1;
        }
}
void deletePerson(struct addbook *book)
{
        cout << "请输入需要删除的联系人" << endl;

        string name;
        cin >> name;

        int ret = isExist(book, name);                                               //这个book传参为啥会报错
}
int main()
{
        struct addbook book;
        book.size = 0;
        int select = 0;
        while (1)
        {
                showMenu();
                
                cin >> select;
                switch (select)
                {
                case 1:addPerson(&book);//利用地址传递修改
                        break;
                case 2: shouPerson(book);
                        break;

                case 3:deletePerson(&book);
                        break;

                case 4:
                        break;

                case 5:
                        break;

                case 6:
                        break;

                case 0:
                        cout << "欢迎下次使用" << endl;
                        system("pause");
                        return 0;
                        break;
                default:
                        break;
                }

        } 
        

        system("pause");
        return 0;
}
最佳答案
2022-2-6 20:03:19
void deletePerson(struct addbook *book)  // 注意这里的 book 是 addbook *
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-6 19:37:19 | 显示全部楼层
改成
int ret = isExist(*book, name);
试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

为什么要传这个,可以解释一下嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-6 20:00:02 | 显示全部楼层
因为你传的book是指针(addbook *),而参数不是指针(addbook),所以你得把book变成*book
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-6 20:03:19 | 显示全部楼层    本楼为最佳答案   
void deletePerson(struct addbook *book)  // 注意这里的 book 是 addbook *
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

        string name;
        cin >> name;

        int ret = isExist(book, name);                                               //这里的形参是一般变量,但book确是指针变量,应该用 *book 替代 book
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-6 14:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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