0

My assignment is to create a general tree, and I decided to do this using three classes: node (contains character data, a pointer to a list ADT which contains all its children, and a pointer to another node for its own sibling nodes), list (contains all the children of a node), and the tree itself. The list is a singly linked list of nodes. The node class is supposed to be used in both the list and tree ADTs. Whenever I try to test my code in main() and see whether the classes are being created properly, I keep getting the error message "error: unknown type name 'list'". Can anyone look at my code and help me figure out how to correctly set up my classes?

//Genghis Khan
#include <iostream>
using namespace std;

class node //will be used for both list and trie ADTs
{
    char character;
    list* child; //all the children of the node will be stored in the list
    node* sibling; //within a list, provides access to all children, functions like a "next"
    friend class list;
    friend class trie;
};
class list
{
private:
    node* head;
    node* tail;
public:
    list()
    {
       head = new node;
        tail = new node;
        head->character = '*'; //head is a sentinel
        tail->character = '*';
        tail->sibling = head;
    }

    void insert(char a) //insertion into the list at the tail
    {
        node* v= new node;
        tail->sibling = v;
        v->character = a;
        v->child = NULL;
        tail = v;
    }

    int size() //so that I can use an empty() boolean
    {
        int size = 0;
        node* temp = head;
        if(temp->character != '*')
        {
            size += 1;
            temp = temp->sibling;
        }
        else
        {
            temp = temp->sibling;
        }
        return size;
    }

    bool empty() //empty condition
    {
        return(size() == 0);
    }

    void print()
    {
        node* temp = head;
        while(temp->character != '*' and temp != tail)
        {
            cout << temp->character;
            temp = temp->sibling;
        }
    }

    friend class trie;
    friend class node;
};
class trie
{
private:
    node* root;
public:
    trie()
    {
        root = new node;
        root->character = '*'; //sentinel
        root->child = NULL; //to be changed when we add children
        root->sibling = NULL; //will never change
    }

    bool haschildren(node* v)
    {
        return(v->child != NULL);
    }


};
int main()
{
    list a;
    a.insert('G');
    a.insert('e');
    a.insert('n');
    a.insert('g');
    a.insert('h');
    a.insert('i');
    a.insert('s');
    a.print();
}

As a note, I tried making "node" a struct instead of a class, but that did not help.

arnavlohe15
  • 332
  • 5
  • 16
  • 1
    You need a forward declaration for the `list` class. – Sam Varshavchik Dec 09 '17 at 23:31
  • When posting questions about build errors, then please copy the build output, as text, in full and complete, then paste it unmodified into the question body. Then mark out the lines in the [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show *where* the errors are. I suggest you take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Dec 09 '17 at 23:31
  • 1) Consider reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). 2) "_I tried making "node" a struct instead of a class_" There is no difference between `struct`, and a `class`, apart from default access specifiers. 3) Is `list` declared at the point of usage (`list* child` in `node`)? And compiler is telling you exactly that - that it isn't. – Algirdas Preidžius Dec 09 '17 at 23:31

0 Answers0