Hi, I'm getting an error message when I try to compile a header file. My
list class (which is defined in one header file) contains a member variable that is a pointer to a variable of type Node (which is another class that is declared in another header file). The compiler is saying Node is private, which it should be. What am I missing?
My textbooks doesn't discuss this in much detail and I can't seem to find an answer on google. If anyone can help explain what I need to include in the list header file so it works, it would be greatly appreciated.
Here are the two files:
//Node.h
class Node
{
int val;
Node* next;
};
//List.h
#include "node.h"
class List
{
Node* head;
};
I took out the other functions to make it shorter to read.
That's exactly what you need to include in the header files, actually, each in its own. As a general rule, in the header files you list declarations and sometimes includes to save typing. In C++ files, you list implementations.
Since your classes need to be declared separately, declare each of them in a different header file. The declarations you wrote could be copied and pasted.
Those classes are going to be quite hard to use, though, seeing as there are no accessors and those members are private.
thanks for the answer. i'm still a little unclear. If the class definition for List includes a pointer to a Node, and the definition of a Node is in a separate file, don't I have to include some way for the compiler to know what a Node is?
//List.h header file:
#include "Node.h"
class List{
Node* head;
};
i took out the other functions to make it quicker to read.