Problem: At various points in working with linked lists, stacks, queues and trees, I have tried to declare the Node struct as a private member of the tree (in this case) class. Everything I can find says that there shouldn't be any issue with this, and it should work the same as any other private member function or variable. When I try to do this, I inevitably get errors from the compiler that read:
tree.cpp:124:28: error: 'struct Tree::Node' is private within this context
void Tree::insert(int val, Node* node)
Am I missing some fine point when it comes to declaring structs as private members of a class? There isn't any particular reason why I can't declare them as public members, but I would like to understand the issue I'm running into.
void Tree::insert(Node* node, int val)
{
if (node == NULL)
{
if (val < node->data)
{
node->llink = new Node(val);
}
elseif (val >= node->data)
{
node->rlink = new Node(val);
}
}
else
{
if (val < node->data)
{
insert(val, node->llink);
}
else
{
insert(val, node->rlink);
}
}
return;
}
Note: I'm still in the middle of debugging this particular project; I'm sure that there are problems with this insert function beyond the specific question I have. Any constructive feedback beyond the scope of my query is warmly welcomed!
If Node is private to a class, then it will be inaccessible outside that class. In any outside-class definition of a member function return types would have to be qualified as Tree::Node rather than just Node. I don't know why that doesn't apply to their argument list as well.