Hello gurus, can you please help me figure out this error:
Goal: Create a Binary Search Tree and add "1" to the root node.
The error I am getting is:
unresolved external symbol "public:_thiscall BST::BST(void)"
#include <iostream>
usingnamespace std;
struct BST {
int data;
BST* root;
BST* left;
BST* right;
//Default Constructor
BST();
BST(int value);
//Insert
BST* Insert(BST*, int);
};
BST::BST(int value) {
//Initialize root, right child and left child
root = nullptr;
right = nullptr;
left = nullptr;
}
//Insert function
BST* BST::Insert(BST* root, int value)
{
//Check if root is non existent
if (root == nullptr) {
//it means node is empty. Assign value to the data value
root->data = value;
}
//Return the value
return root;
}
int main() {
//Create object
BST mybst, * root = nullptr;
//use method insert here
mybst.Insert(root, 1);
}
^^ I don't see why you wanted to do that here (it works, but so would just making his ctor body and fixing the issues mentioned). Do you mind explaining?
Jonnin, I agree with dutch. The OP has data, right, left, and root in the BST class. A node doesn't need a pointer to root, and a tree doesn't need data, right or left.
mrpear2020, line 30 checks for a nullptr. Line 32 then accesses the pointer that is guaranteed to be null. That won't work....
I think you must not have looked at the situation closely enough. As dhayden said there is no reason to have a root pointer in every single node. And an overall class for the tree (list, queue,...) is always a good idea. Here it is filled out a little more: