Status of post: Unsolved!!! - type conversion problem that needs help C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slizorn
    New Member
    • Jul 2008
    • 34

    Status of post: Unsolved!!! - type conversion problem that needs help C++

    hi guys,
    the error is as follows:
    error C2440: '=' : cannot convert from 'TreeNode<T> *' to 'char *'
    with
    1> [
    1> T=char
    1> ]
    1> Types pointed to are unrelated; conversion requires reinterpret_cas t, C-style cast or function-style cast

    i get this error from the following line of code:
    Code:
    assign = searchTree(treeObj->root ,data1);
    which is part of the following method
    Code:
    void handleOneLine(string string1)
    {
    	char * cstr, *p, *assign;
    	int counter;
    	string str (string1);
    	char data1, data2, data3;
    
    	cstr = new char [str.size()+1];
    	strcpy (cstr, str.c_str());
    
    	// cstr now contains a c-string copy of str
    
    	int count = 0;
    	p=strtok (cstr,",");
    	count++;
    	while (p!=NULL)
    	{
    		p=strtok(NULL,",");
    		if( count == 1 )
    		{			
    			data1 = *p;
    			assign = searchTree(treeObj->root ,data1);
    			treeNodeObj->item = new TreeNode();
    		}
    		else if( count == 2 )
    		{
    			data2 = *p;
    //			treeNodeObj->setLeft(data2);
    		}
    		else if( count == 3 )
    		{
    			data3 = *p;
    //			treeNodeObj->setRight(data3);
    		}
    		count++;
    		if( count == 3 )
    			break;
    	}
    
    	delete[] cstr; 
    }
    the searchTree method is below
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <queue>
    #include <deque>
    #include "TreeNode.h"
    #include "Tree.h"
    using namespace std;
    
    Tree<char> *treeObj = NULL;
    TreeNode<char> *treeNodeObj = NULL;
    
    TreeNode<char>* searchTree(TreeNode<char> *cur, char nodeToAdd)
    {
    	if(cur == NULL) 
    	{
    		return NULL;
    	}
    	if(cur->IsSameNode(nodeToAdd))
    	{
    		return cur;
    	}
    	searchTree(cur->getLeft(), nodeToAdd);
    	searchTree(cur->getRight(), nodeToAdd);
    }
    could you please help me with this?
    i will reply within 5 mins so plz do check back if u require anything else e.g. code or a qn answered.
    thanks :)

    i have included the TreeNode file below just in case:
    Code:
    #pragma once
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <deque>
    #include <queue>
    using namespace std;
    
    template <class T>
    class TreeNode
    {
    	public:
    		TreeNode<T>(T newItem);
    		~TreeNode<T>(void);
    		void setItem(T *newItem);
    		void setLeft(TreeNode *newLeft);
    		void setRight(TreeNode *newRight);
    		T getItem();
    		TreeNode<T>* getLeft();
    		TreeNode<T>* getRight();
    		bool IsSameNode(T c);
    		T item;
    
    	private:
    		TreeNode *left;
    		TreeNode *right;
    };
    
    
    template <class T>
    TreeNode<T>::TreeNode(T newItem)
    {
    	item = newItem;
    	left = null;
    	right = null;
    }
    
    template <class T>
    TreeNode<T>::~TreeNode(void)
    {
    }
    
    
    template <class T>
    void TreeNode<T>::setItem(T *newItem) 
    {
    	// set methods
    	item = newItem;
    }
    
    template <class T>
    void TreeNode<T>::setLeft(TreeNode *newLeft) 
    {
    	left = newLeft;
    }
    
    template <class T>
    void TreeNode<T>::setRight(TreeNode *newRight) 
    {
    	right = newRight;
    }
    
    template <class T>
    T TreeNode<T>::getItem()
    {
    	// get methods
    	return item;
    }
    
    template <class T>
    TreeNode<T>* TreeNode<T>::getLeft() 
    {
    	return left;
    }
    
    template <class T>
    TreeNode<T>* TreeNode<T>::getRight() 
    {
    	return right;
    }
    
    template <class T>
    bool TreeNode<T>::IsSameNode(T c)
    {
    	return this->item == c;
    }
  • slizorn
    New Member
    • Jul 2008
    • 34

    #2
    please feel free to ask me any qns or suggest any suggestions.. i am up for anything.. thanks :)

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      Hi,
      I'm not sure this is helpful, but here it is anyway.
      Your compiler is complaining because the searchTree function returns a TreeNode<char>* , and you can't assign that to a variable of type char*.
      I didn't look at your code very carefully, but maybe you meant assign to be of type TreeNode<char>* , like this:
      Code:
      TreeNode<char>* assign;
      assign = searchTree(treeObj->root ,data1);
      or maybe you meant to assign to be of type char, and to use the TreeNode's item variable:
      Code:
      char assign;
      assign = searchTree(treeObj->root ,data1)->item;
      Well, I hope this is somewhat helpful. Good luck.

      Comment

      • slizorn
        New Member
        • Jul 2008
        • 34

        #4
        Originally posted by boxfish
        Hi,
        I'm not sure this is helpful, but here it is anyway.
        Your compiler is complaining because the searchTree function returns a TreeNode<char>* , and you can't assign that to a variable of type char*.
        I didn't look at your code very carefully, but maybe you meant assign to be of type TreeNode<char>* , like this:
        Code:
        TreeNode<char>* assign;
        assign = searchTree(treeObj->root ,data1);
        or maybe you meant to assign to be of type char, and to use the TreeNode's item variable:
        Code:
        char assign;
        assign = searchTree(treeObj->root ,data1)->item;
        Well, I hope this is somewhat helpful. Good luck.

        yup it was cheers! :)

        Comment

        Working...