Error in binary tree

This is a discussion on Error in binary tree within the C++ Programming forums, part of the General Programming Boards category; I do not know what I am missing in my code that causes this following error: Code: Error E2285 TreeProgram.cpp ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    Error in binary tree

    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;
    }
    Last edited by aama100; 03-16-2008 at 05:50 PM.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You haven't defined a Tree constructor that takes a string parameter, the line of code in questions is passing a string!

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I have updated the code above and added the constructor to it, so I got these following errors that don't make sense to me:

    Code:
    Error E2171 TreeProgram.cpp 20: Body has already been defined for function 'Tree::Tree()'
    Error E2015 TreeProgram.cpp 23: Ambiguity between 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator =(const char *)' and 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator =(char)' in function Tree::Tree()
    Error E2285 TreeProgram.cpp 44: Could not find a match for 'Tree::Tree(string)' in function Tree::InsertTree(Tree * &,string)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How did you actually update the code? Do you now have a constructor with a signature like Tree::Tree(string Item) ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21