0

Here is my code so far. ELEMENT is a class object that so far isn't giving me any trouble:

struct HEAP {
    public:
         int size;
         static int capacity;
         ELEMENT H[capacity];
         HEAP Initialize(int capacity);
}

HEAP HEAP::Initialize(int capacity) {
     ELEMENT * myHeap;
     myHeap = new ELEMENT [capacity];
}

int n;
n = 3;
Initialize(n);

I am new to C++ so I think I am unclear with how I am supposed to use class functions. Basically what I am trying to do is to initialize an array of pointers to element objects, and call that a heap. How am I using C++ functions incorrectly here? I'm used to Java so this feels a bit foreign to me.

  • 1
    This code is setup all wrong. `Initialize()` should be changed into a proper constructor, `H` needs to be changed to a pointer instead of a fixed array and then `myHeap` assigned to it, and you need to declare a `HEAP` object to construct/initialize in the first place. I suggest you get a [good C++ book](https://stackoverflow.com/questions/388242/) to learn from. – Remy Lebeau Mar 20 '20 at 03:06
  • Your C++ book should cover this material very thoroughly. If there's something in your book that you don't understand, you are welcome to post a question here with a brief cite, and a brief note explaining what is unclear to you. Unfortunately, stackoverflow.com is not a replacement for a good C++ book, and it is not a crowdsourced C++ tutorial site. – Sam Varshavchik Mar 20 '20 at 03:10
  • Why `Initialize`? Why not constructor? – Eljay Mar 20 '20 at 03:29
  • Thank you guys for the feedback! I appreciate it. – Brittany May Mar 20 '20 at 03:39

0 Answers0