簡體   English   中英

鏈表中的結構

[英]Structure in a linked list

我有一個程序,在鏈表中實現一個結構。 我在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_
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM