-3

I have a dynamic template array as a member of my class. However, I can't resize the array in the constructor or in any other functions. I'm confused on the syntax. Here's the code:

template <class Type> class MaxHeapTree {
private:
    HeapNode<Type> *array[];
    HeapNode<Type> *root;
    int elementSize;
    int height;
    int leafCounter;
public: 
    // Constructor
    MaxHeapTree(int n = 10) : elementSize(0), height(0), leafCounter(0) {
        HeapNode<Type> *array = new HeapNode<Type>[n];
    }

The array is a an array of HeapNode<Type> objects included from the HeapNode class. Here's the constructor for the HeapNode class:

template <class Type> class HeapNode {
private:
    int key;
    Type value;
public:
    HeapNode(int key, Type const &value) {
        this->key = key;
        this->value = value;
    }
luigi741
  • 5
  • 1

2 Answers2

2

There are multiple problems with the shown code.

HeapNode<Type> *array[];

As described, this should be declared, simply:

HeapNode<Type> *array;

Then, in the constructor:

HeapNode<Type> *array = new HeapNode<Type>[n];

This declares a variable in the constructor function that's called "array". This does absolutely nothing to initialize a class member of that name. The constructor should simply be:

MaxHeapTree(int n = 10) : array(new HeapNode<Type>[n]), elementSize(0),
                          height(0), leafCounter(0)
{
}

Presumably, the array's size, n, should also be stored somewhere. But that part wasn't shown, in the question.

Furthermore, I would also question even the need to use dynamic allocation here. I see nothing here that cannot be accomplished by using a std::vector, in place of the dynamically-allocated array. Modern C++ code rarely needs to new or delete anything, especially arrays. Standard C++ containers eliminate the need for dynamic allocation in most cases. If std::vector was used here right from the beginning, this issue would not've happened in the first place.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I was tasked with using a dynamic array to create a heap tree. However, I've tried that initialization before and it won't compile. – luigi741 Jul 04 '16 at 23:57
  • 2
    "It won't compile" is not a useful problem description. – Sam Varshavchik Jul 05 '16 at 00:03
  • `MaxHeapTree.h: In instantiation of ‘MaxHeapTree::MaxHeapTree(int) [with Type = std::__cxx11::basic_string]’: main.cpp:14:61: required from here MaxHeapTree.h:22:98: error: no matching function for call to ‘HeapNode >::HeapNode()’ xHeapTree(int n = 10) : array(new HeapNode[n]), elementSize(0), height(0), leafCounter(0)` – luigi741 Jul 05 '16 at 00:13
  • HeapNode.h:14:2: note: candidate: HeapNode::HeapNode(int, const Type&) [with Type = std::__cxx11::basic_string] HeapNode(int key, Type const &value) { ^ HeapNode.h:14:2: note: candidate expects 2 arguments, 0 provided HeapNode.h:9:29: note: candidate: HeapNode >::HeapNode(const HeapNode >&) template class HeapNode { – luigi741 Jul 05 '16 at 00:13
  • This is because your `HeapNode` class does not have a default constructor. This has nothing to do with the array construction. You have to fix this, a separate issue, which has absolutely nothing to do with `MaxHeapTree`. No matter how you go about constructing an array, in `MaxHeapTree`'s constructor, it will not be possible until you fix this, too. – Sam Varshavchik Jul 05 '16 at 00:18
  • I've added a default constructor to the HeapNode class but it throws In function `MaxHeapTree, std::allocator > >::MaxHeapTree(int)': main.cpp:(.text._ZN11MaxHeapTreeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC2Ei[_ZN11MaxHeapTreeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC5Ei]+0x75): undefined reference to `HeapNode, std::allocator > >::HeapNode()' collect2: error: ld returned 1 exit status – luigi741 Jul 05 '16 at 00:33
  • This means you declared the default constructor, but did not define it. – Sam Varshavchik Jul 05 '16 at 00:37
  • Don't forget that template classes' constructor and destructor must be defined in the header file. – Rezniaq Jul 05 '16 at 03:05
1

Use a container to manage it:

std::vector<HeapNode<Type>> array
rflobao
  • 562
  • 2
  • 8