Normally, when you don't have any other constructors defined, the compiler makes a default one for you automatically, but since you have another constructor, you'll have to define the default constructor yourself:
1 2 3
BinaryTree() { /* ... */}
// Alternatively, you could just tweak your existing constructor:
BinaryTree(ElementType new_data = /* some default value */) { root = new Node(new_data); }
I switched the two even though that shouldn't make a difference. Error still persists.
The problem I'm having is that I need to pass an initial piece of data to my constructor for it to run properly, but I want to leave it empty from the start and put things in later. The insert function isn't the issue here. In the constructor, new_data is given an initial value of NULL, but that isn't working.
If it's std::string (which I suspect it is), then in your Node default constructor, you try to construct data with NULL, which doesn't work.
(You should get the same error from just doing std::string str(NULL);.)
In general, I'd avoid setting anything that's not a pointer to NULL.
Try changing Node(ElementType new_data = NULL)
to Node(ElementType new_data = ElementType())
and see if that helps.
(If your BinaryTree default constructor also tries to set new_data to NULL, change that as well.)
Really?
I copied/pasted your code and made the changes, and it works for me.
To clarify:
11 12 13 14 15 16 17 18
//Node(ElementType new_data = NULL)
Node(ElementType new_data = ElementType())
{
data = new_data;
left = NULL;
right = NULL;
count = 0;
};
22 23 24
BinaryTree(ElementType new_data = ElementType()) { root = new Node(new_data); };
// Or you could have something like this added instead:
BinaryTree() { root = new Node(); }
1 2 3 4 5 6
void BinaryTree::insert(ElementType new_data, Node *&root)
{
if (root == NULL) // Swapped this with the next one
root = new Node(new_data);
elseif (root->data == new_data)
root->count++;