【问题标题】:I keep getting an error when I try to pass the head pointer of the linked list当我尝试传递链表的头指针时,我不断收到错误消息
【发布时间】:2021-02-21 14:42:15
【问题描述】:

我不断收到错误消息 undefined symbol insert(*Friend)。任何人都可以识别并解释错误吗?我知道它指的是它找不到它但我无法确定问题所在,请帮助。我正在开发一个使用链表保存朋友信息数据库的程序。因此,我试图在程序启动后将头节点初始化为null,并检查第一个插入是否此头是null,如果是这种情况,则将指针分配给新节点。

#include <iostream>

using namespace std;

// MARK: Friend Struct

struct Friend {

    string last;
    string first;
    int yob;
    int mob;
    int dob;
    string sex;
    struct Friend* next;
};

Friend* head = NULL;
void insert(struct Friend* head);
void getData();

// MARK: Main Method
int main(int argc, const char* argv[])
{
    insert(head);

    return 0;
}

// MARK: Get Data Method

void getData(string& f, string& l, string& s, int& y, int& m, int& d)
{

    cout << "Please type friend's first name: " << endl;
    cin >> f;
    cout << "Please type friend's last name: " << endl;
    cin >> l;
    cout << "Please type friend's year of birth (yyyy): " << endl;
    cin >> y;
    cout << "Please type friend's month of birth (mm): " << endl;
    cin >> m;
    cout << "Please type friend's day of birth (dd): " << endl;
    cin >> d;
    cout << "Please type friend's sex (f/m): " << endl;
    cin >> s;
}

// MARK: Insert new Friend Method

void insert(struct Friend** head)
{
    string f, l, s;
    int y = 0, m = 0, d = 0;
    getData(f, l, s, y, m, d);

    Friend* tmp = new Friend;
    tmp->first = f;
    tmp->last = l;
    tmp->yob = y;
    tmp->mob = m;
    tmp->dob = d;
    tmp->sex = s;
    tmp->next = NULL;

    if (*head == NULL) {
        *head = tmp;
    }
};

【问题讨论】:

    标签: c++ pointers struct linked-list


    【解决方案1】:

    你声明了void insert(struct Friend* head);(只有一颗星),但只定义了void insert(struct Friend** head)(有两颗星)。它们是不同的功能。

    似乎应该将声明更正为两颗星(与定义匹配)并且在调用时应该传递&amp;head 而不是head

    【讨论】:

      【解决方案2】:

      您将 (Friend*)head 传递给 insert(Friend **head),这是不正确的。 将呼叫更改为insert(&amp;head);

      【讨论】:

        猜你喜欢
        • 2014-05-03
        • 2019-07-19
        • 2020-07-26
        • 2022-11-28
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-29
        相关资源
        最近更新 更多