Reputation: 25
I am working on a BST. The relevant code is given below:- This is the class definition:-
class BST
{
private:
struct Leaf
{
int val;
int count;
Leaf *left,*right;
};
Leaf *root; // This is unique for entire class;
public:
BST() // Constructor for BST
{
root = NULL;
}
void Insert(); // Insert in the BST
Leaf* InsertNode(Leaf *pos,Leaf *node); // Recursive and Consize function to Insert the node,extention of Insert func
void Search(); // Search in the BST
bool SearchNode(Leaf *pos,int val); // Recursive and Consize function to Search the node,extention of Search func
int Count(Leaf *Node); // Count the children of a Node
void Print(); // Print the BST
void Inorder(Leaf *Node,int indent);
void Delete(); // Delete a node in BST
Leaf* DeleteNode(Leaf *pos,int val); // Recursive and Consize function to Delete the node,extention of Delete func
};
This is the definition of the Insert function:-
Leaf* BST::InsertNode(Leaf *pos,Leaf *node)
{
if(pos == NULL)
{
return node;
}
if(pos->val > node->val)
pos->left = InsertNode(pos->left,node);
if(pos->val < node->val)
pos->right = InsertNode(pos->right,node);
return pos;
}
I am getting the compilation error:-
bst.cpp(81): error C2143: syntax error : missing ';' before '*'
bst.cpp(81): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
bst.cpp(82): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
bst.cpp(82): error C2556: 'int *BST::InsertNode(BST::Leaf *,BST::Leaf *)' : overloaded function differs only by return type from 'BST::Leaf *BST::InsertNode(BST::Leaf *,BST::Leaf *)'
The error is still present even if I make declarion of Structure public. The error disappears only when I declare the struct outside class.
Can someone please explain what is the reason behind this?
Upvotes: 1
Views: 144
Reputation: 227418
The issue is not access control, but rather scoping. Leaf
is defined in BST
, so its name is in the BST
scope:
BST::Leaf* BST::InsertNode(BST::Leaf *pos, BST::Leaf *node)
Now, being a private type of BST
places some limitations on how non-friends can access the type. They won't be able to say
BST::Leaf* leaf = foo.InsertNode(bar, baz);
but they can say
auto leaf = foo.InsertNode(bar, baz);
On the surface it looks like BST::Leaf
should be public
.
Upvotes: 1