gpt4 book ai didi

c++ - 链表中的结构

转载 作者:行者123 更新时间:2023-11-30 02:51:52 25 4
gpt4 key购买 nike

我有一个程序在链表中实现一个结构。我在 main 中收到一个错误,说“无效使用 struct Node::date”。我想不通。我的教授们也不知道。任何帮助和解释都将不胜感激,这样我就知道它为什么这样做了。

#include <iostream>
#include <cstddef>
#include <string>
using namespace std;

struct date
{
int day;
int month;
int year;
};

struct Node
{
string item;
int count;
Node *link;
struct date;
};

typedef Node* NodePtr;
void head_insert(NodePtr& head, string an_item, int a_number, date a_date);
void show_list(NodePtr& head);

int main()
{
date tea_date, jam_date, rolls_date;
rolls_date.day = 8;
rolls_date.month = 10;
rolls_date.year = 2003;

jam_date.day = 9;
jam_date.month = 12;
jam_date.year = 2003;

tea_date.day = 1;
tea_date.month = 1;
tea_date.year = 2010;

NodePtr head = NULL;
head_insert(head, "Tea", 2, tea_date);
head_insert(head, "Jam", 3, jam_date);
head_insert(head, "Rolls", 10, rolls_date);

show_list(head);
system("PAUSE");
return 0;
}

void head_insert(NodePtr& head, string an_item, int a_number, date a_date)
{
NodePtr temp_ptr;

temp_ptr = new Node;
temp_ptr-> item = an_item;
temp_ptr-> count = a_number;
temp_ptr-> date = a_date;

temp_ptr->link = head;
head = temp_ptr;
}

void show_list(NodePtr& head)
{
NodePtr here = head;

while (here != NULL)
{
cout << here-> item << "\t";
cout << here-> count << endl;

here = here->link;
}
}

最佳答案

这只是一个名为 datestruct 的声明:

struct date;

你需要给你的 Node 一个 date 实例:

struct Node
{
string item;
int count;
Node *link;
date date_; // Node has a date called date_
};

关于c++ - 链表中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19407005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com