I have a project on linked lists where a Sentence is a linked list of Words, a Paragraph a LL of Sentences, and a Story a LL of Paragraphs. I've tried looking through books and online on how to do this, but the way I'm forced to do this project is so much different from the examples I can't make any sense of it. Each class is broken up into a .cpp and .h file. I can't find any example like this. The part that trips me up the most is that the header files need a forward declaration. That is, the Story class has one or more functions that take Paragraphs as parameters. The implementation of the Paragraph class depends on Sentence, which uses Word, but two words can be combined to make a Sentence and the implementation of that combination goes in the Word class.
So my .cpp files just *include their .h files. Word.h #includes Sentence.h, Sentence.h #includes Paragraph.h and Word.h, Paragraph.h #includes Story.h and Sentence.h, and Story.h #includes just Paragraph.h.
When I try to make a Node in the header files I get an incomplete data type. For example, if in Story.h I try to do
1 2 3 4
|
struct Node{
Paragraph data;
Node * next;
};
|
I get field 'data' has incomplete type. I can get around this by making data a pointer to a Paragraph type. So Paragraph * data;
So in my constructor for Story() I have
1 2
|
Node node;
node.data = new Paragraph();
|
But then I have no idea how to delete node.data in the destructor because I get 'node' not declared in this scope. Also when I try to do Node node; in a member function I get an error 'Node' was not declared in this scope. I am beyond confused and completely overwhelmed at this point. All I have to do is create linked lists and add things to them either from the front or back. But I have no idea why things are going out of scope. This would be so much easier if I didn't have to have Class * data and just use Class data like all the other LL examples do. Is there a way I could use like friend functions or something and get around forward declarations? Also why is my Node not in scope? Any help would be greatly appreciated