I'm a beginner at coding. Double link list. Assignment question

Asked 2 years ago, Updated 2 years ago, 22 views

Hello, I made a code that uses a double connection list to input Corona information by country and print it out, but there are dozens of compilation errors I don't know what the problem is Attached is the code

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

Node* g_head = NULL;
Node* g_tail = NULL;
int g_count = 0;

class Node {
public:
    string m_nation = "";
    int m_occur = 0;
    int m_deadman = 0;
    double m_mortality = 0;

    Node* next = NULL;
    Node* prev = NULL;



    Node(string nation = "", int occur = 0, int deadman = 0, Node* left = NULL){
        m_nation = nation;
        m_occur = occur;
        m_deadman = deadman;
        m_mortality = deadman / occur;

        if (g_count == 0) {
            g_head = this;
            g_tail = this;
            g_count++;
        }
        else {
            g_tail = this;
            prev = left;
            prev->next = this;

            g_count++;
        }
    }

private:

protected:

};
void print_all_list() {
    Node* pt = g_head;
    for (int i = 0; i < g_count; i++) {

        cout << pt->m_nation << " " << pt->m_occur << " " << pt->m_deadman << " " << pt->m_mortality << endl;
        pt = pt->next;
    }
}
int main(void) {

    Node America("America", 43214, 533);
    Node China("China", 81711, 3277,&America);
    Node France("France", 19856, 869, &China);
    Node Germany("Germany", 29056, 123, &France);
    Node Iran("Iran", 23049, 1812, &Germany);
    Node Italy("Italy", 63927, 6077, &Iran);
    Node Korea("Korea", 9037, 120, &Italy);
    Node Spain("Spain", 33098, 2182, &Korea);

    print_all_list();

    return 0;
}

c++

2022-09-21 10:24

1 Answers

There is a compilation error like below.

/solution0.cpp:5:1: error: unknown type name 'Node'
Node* g_head = NULL;
^
/solution0.cpp:6:1: error: unknown type name 'Node'
Node* g_tail = NULL;

To use a type name in C++, it must already be declared. Node*g_head = NULL; is above the definition of classNode. In other words, Node does not exist at the time g_head is declared. To resolve this issue, you must either use a forward declaration or move g_head and g_tail to the lower line of the Node definition. However, the Node constructor uses two global variables, so I'll make a forward declaration. Change to classNode*g_head = NULL; and classNode*g_tail = NULL;.


2022-09-21 10:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.