I thought I understood how pointers and objects work, but I am confused when this happened to me. I was not able to create an object using the new operator. I had to create a pointer to the class and assign the new object to the pointer. Attaching the code below
It is as error states. new returns a pointer to the object in dynamic memory.
On line 19 you declare llTree to be object, not a pointer to object. So you cannot assign a pointer to object.
BST obj; // this creates a BST object named 'obj'
BST* ptr; // this creates a pointer named 'ptr', but not a BST object
// Once initialized, the pointer merely contains an address to an existing object
ptr = &obj; // now, 'ptr' points to our 'obj' object
// 'new' works differently. Rather than creating a named object, it creates an UNNAMED
// object, and gives you a pointer to it.
BST* p2 = new BST; // 'new BST' creates an unnamed BST object. For this example, let's call
// this object 'foo'. The new keyword will also return a pointer to the created object, so
// after this line, p2 will point to 'foo'
// so now we have 2 objects: obj and foo
// and we have 2 pointers: ptr (points to obj), and p2 (points to foo)
// The 'delete' keyword is slightly deceptive. You must only delete what you created
// with new:
delete p2; // <- this does not actually delete p2. It will delete whatever p2 points to.
// so in fact, this will delete 'foo'. p2 remains a pointer just as it was before, the
// only difference is the object it pointed to no longer exists.