Jack Smother
Jack Smother

Reputation: 207

binary search tree, non recursive using stack

What should I know about having two separate classes in one .h file?

I have a binary search tree class with all the members and public & private functions.

class BinarySearchTree
{
   struct Node {
      Node* left;
      Node* right;
      int val;
   };
};

and following that code I want to design a stack of pointers to that binary search tree node. Within the same.h file I have

class stack
{
  Node* array;
  //

};

Visual Studio doesn't show linkage and doesn't recognize Node*. Is it ok to declare two separate classes in one .h file or is it better to implement the stack class nested inside the binary search tree class?

Upvotes: 0

Views: 312

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26259

You've declared a struct called Node that is nested in the class BinarySearchTree, so if you want to refer to that struct outside of the class, you need to refer to it like this:

class stack
{
  BinarySearchTree::Node* array;
  //

};

Whether or not that's good design is a whole new question, so I would recommend getting a bit further with the implementation before asking more.

EDIT

Like you noticed, it's necessary to make the nested struct public if you want to use it outside of the class. That, in itself, is not necessarily bad or wrong. You're not exposing data, just a declaration.

You've got two choices:

  1. Make the nested struct public.
  2. Take the nested struct outside of the enclosing class.

Personally, I'd go for the first option.

Upvotes: 1

Related Questions