0

The last couple of days I've been trying to figure out how to make a dynamically allocated array of this class I made. One of them includes another class I made

Here are the classes:

template<typename T>
class BST // a binary search tree class
{
private:
    Node<T> * root;
    int size;
public:
    BST()
    {
        root = NULL;
        size = 0;
    }

/*void * BST::operator new[](size_t a) //tried it both with the "BST::" and without
    {
        void * p = malloc(a);//24
        return p;
    }*/
//there are more functions that i cut out
}

and

template <typename T>
class Node //a node for a binary search tree
{
public:
    int key;
    T data;
    Node * left;
    Node * right;
    Node * parent;
//public:
    Node<T>()
    {
        key = NULL;
        data = NULL;
        left = NULL;
        right = NULL;
        parent = NULL;
    }
//more stuff below this but it's just getting and setting the data
}

in main() i'll try to make an array of BST objects with this line:

BST<char> * t = new BST<char>[ n ]; //the user will give the value for int n

The issue is that it only makes one BST object when running. I've done some research and experimented with overloading the new[] operator which did absolutely nothing.

Can someone please explain what the proper way to do this would be?

7
  • Do you want to allocate all nodes at the start, or do you want to grow the tree dynamically whenever a new item is added to it? Also, you should use the initializer syntax in the constructor for Node, since with the current design you limit T to types which can be assigned to NULL. Commented Jul 28, 2017 at 6:21
  • "The issue is that it only makes one BST object when running" It makes n objects. Are you saying n == 1? Commented Jul 28, 2017 at 6:24
  • @juanchopanza I'll set n equal to 3 and it will only ever make one object. Commented Jul 28, 2017 at 6:28
  • 2
    @ChrisDouglas Unlikely unless the code crashes after one object. minimal reproducible example or it didn't happen. Commented Jul 28, 2017 at 6:30
  • @Rudi When I first make a BST object the root node should be null and objects can be inserted after that. Allocating a Node object doesn't pose any problem though. It's just when i try to make an array of BST objects :) Commented Jul 28, 2017 at 6:35

1 Answer 1

1

You do have more than one object in the array, but t is not an array.

t is a pointer to one BST, and the debugger displays it as such – the debugger has no idea that it's a pointer to the first element of an array.

If you want to view it as an array, you need to tell the debugger to do that.
You do this in the "Watch" window, and I think the syntax would be t,2 for displaying the first two elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.