I do not know what I am missing in my code that causes this following error:
Code:
Error E2285 TreeProgram.cpp 40: Could not find a match for 'Tree::Tree(string)' in function Tree::InsertTree(Tree * &,string)
This is my code.
Code:
#include <iostream>
#include <string>
using namespace std;
class Tree
{
public:
int item;
Tree* root;
Tree* left;
Tree* right;
int countTree( Tree* root );
void InsertTree(Tree *&root, string newItem );
};
Tree::Tree(){
root = new Tree;
root->item = 0;
root->left = NULL;
root->right = NULL;
}
int Tree::countTree( Tree* root ) {
if ( root == NULL )
return 0;
else {
int count = 1;
count += countTree(root->left);
count += countTree(root->right);
return count;
}
}
void Tree::InsertTree(Tree *&root, string newItem) {
if ( root == NULL ){
root = new Tree( newItem ); // <<<<<<<<<<<HERE IS THE PROBLEM <<<
return;
}
else if ( newItem < root->item ) {
InsertTree( root->left, newItem );
}
else {
InsertTree( root->right, newItem );
}
}
int main ()
{
Tree* root;
int loop=0;
int num;
while(loop == 0){
cout << " Select an Option:" << endl;
cout << "(1) Enter a new calculation string." << endl;
cout << "(2) Print the current tree in infix notation." << endl;
cout << "(3) Print the current tree in prefix notation." << endl;
cout << "(4) Print the current tree in postfix notation." << endl;
cout << "(5) Calculate the result of the current tree." << endl;
cout << "(6) Exit the program." << endl;
cout << " " << endl;
cin >> num;
if ( num ==6){
break;
}
}
return 0;
}