Thread: Binary Tree 911

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    Binary Tree 911

    Hi all I'm so close to getting this stupid binary tree to work except I keep getting some wack ass error message. It says
    and I quote error C2440: '=' : cannot convert from 'char' to 'char12]'
    observe the code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    struct node
    {
        char info[12];
        node *left;
        node *right;
    };
    
    								//The line below should be in main and you should probably
    								//not be allocating memory for a new node at this point,
    								//that will be done in the insert function, you
    								//should just set it to NULL in main
    								// Forgot to add the return types for the two functions below
    void preorder(node *p);
    void inorder(node *p);		    //is this the correct syntax for declaring the function?
    
    void insert(node *&p, char string[12]);   //for user defined functions using pointers?
    
    void insert(node *&p, char string[12])    // This differs from the function prototype you provided above
    { 
        if(p==NULL)
        {
            p=new(node); 
            p->info = string[12];   
            p->left=NULL;
            p->right=NULL;
        }
        else if(string < p->info) 
        {
            insert(p->left,  string);
        }
        else   insert(p->right, string);
    }  // You were missing an ending bracket, add this
    
    void inorder(node *p)
    {
        if(p!=NULL)
        {
            inorder(p->left);
            cout<<p->info<<"  ";
            inorder(p->right);
        }
    
    }
    void preorder(node *p)
    {
    	
    	if(p!=NULL)
    	{
    		cout<<p->info<<"  ";
    		preorder( p->left);
    		preorder(p->right);
    	}
    }
    
    
    int main() // main should always return an int
    {
        node *root=NULL;
    	char string[12];
    	for(int i=0; i<6; ++i)
    	{
    		cout<<"Enter the value for your node: ";
    		 cin.getline(string, 11, '\n');
    		insert(root, string[12]); // Your insert function takes two parameters but you only provide one
    	}
    	inorder(root);
    	cout<<endl;
    	preorder(root);
    	return 0;
    }
    anyhow could anyone out there give me some feedback on where I'm going wrong here.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > insert(root, string[12]); // Your insert function
    To pass an array to a function, you just write it's name

    insert(root, string);

    > error C2440: '=' : cannot convert from 'char' to 'char12]'
    Well yes, because string[12] is an element of the array (a char), and string is the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11: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, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM