Question

my node class for a LLL, and a class derived from it

class node
{
    public:
    node();
    ~node();
    node *& getNext();
    protected:
    node * next;
};

class word: public node
{
    public:
    word();
    ~word();
    protected:
    char ** words; //an array of char pointers
};

and I have another class

class sentence
{   
    public:
        sentence(); 
        ~sentence();
        void insert(node *& head, char *& word_to_add);
        sentence *& operator+=(char *& sentence_to_add);
        void remove(char *& word_to_remove);
        void testing(sentence *& s);
        void display();
        void removeAll();
    protected:
        node * head;
};

and here's my implementation of the insert function

void sentence::insert(node *& head, char *& word_to_add)
{
    if(!head)
    {
        head = new word;
        insert(head, word_to_add); 
    }
    else
    {

        for(int i = 0; i < MAX_WORD; ++i)
        {
            if(!head->words[i])
            {
                head->words[i] = new char[strlen(word_to_add)+1];
                strcpy(head->words[i], word_to_add);
                cout << head->words[i] << endl;
                return;
            }
        }
        insert(head->getNext(), word_to_add); 
    }
}

I'm getting the error class node has no data member named words. But if I were to change the prototype void insert(node *& head, char *& word_to_add); into void insert(word *& head, char *& word_to_add); it would say that word has no data member next. I'm kinda stuck because I did this in a previous program and it worked just fine. I was wondering what I'm doing wrong, so if someone could point that out, that'd be great!

edit: I forgot to include that I'm using the friend keyword in class word. For testing purposes. friend class sentence;

Was it helpful?

Solution

I would not derive from the node class.

Have the node be generic and contain pointer to some data (word) instead of having the data directly in the node ?

class node
{
  node* next;
  word data;
};

class word
{
   char** words;
};

Since you tagged this C++, I might suggest you use a standard C++ collection (std::vector) instead of a linked list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top