Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Member
 
Join Date: Jul 2008
Posts: 34
#1: Sep 6 '08
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_cast, C-style cast or function-style cast

i get this error from the following line of code:
Expand|Select|Wrap|Line Numbers
  1. assign = searchTree(treeObj->root ,data1);
  2.  
which is part of the following method
Expand|Select|Wrap|Line Numbers
  1. void handleOneLine(string string1)
  2. {
  3.     char * cstr, *p, *assign;
  4.     int counter;
  5.     string str (string1);
  6.     char data1, data2, data3;
  7.  
  8.     cstr = new char [str.size()+1];
  9.     strcpy (cstr, str.c_str());
  10.  
  11.     // cstr now contains a c-string copy of str
  12.  
  13.     int count = 0;
  14.     p=strtok (cstr,",");
  15.     count++;
  16.     while (p!=NULL)
  17.     {
  18.         p=strtok(NULL,",");
  19.         if( count == 1 )
  20.         {            
  21.             data1 = *p;
  22.             assign = searchTree(treeObj->root ,data1);
  23.             treeNodeObj->item = new TreeNode();
  24.         }
  25.         else if( count == 2 )
  26.         {
  27.             data2 = *p;
  28. //            treeNodeObj->setLeft(data2);
  29.         }
  30.         else if( count == 3 )
  31.         {
  32.             data3 = *p;
  33. //            treeNodeObj->setRight(data3);
  34.         }
  35.         count++;
  36.         if( count == 3 )
  37.             break;
  38.     }
  39.  
  40.     delete[] cstr; 
  41. }
  42.  
the searchTree method is below
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <queue>
  5. #include <deque>
  6. #include "TreeNode.h"
  7. #include "Tree.h"
  8. using namespace std;
  9.  
  10. Tree<char> *treeObj = NULL;
  11. TreeNode<char> *treeNodeObj = NULL;
  12.  
  13. TreeNode<char>* searchTree(TreeNode<char> *cur, char nodeToAdd)
  14. {
  15.     if(cur == NULL) 
  16.     {
  17.         return NULL;
  18.     }
  19.     if(cur->IsSameNode(nodeToAdd))
  20.     {
  21.         return cur;
  22.     }
  23.     searchTree(cur->getLeft(), nodeToAdd);
  24.     searchTree(cur->getRight(), nodeToAdd);
  25. }
  26.  
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:
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <deque>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. template <class T>
  10. class TreeNode
  11. {
  12.     public:
  13.         TreeNode<T>(T newItem);
  14.         ~TreeNode<T>(void);
  15.         void setItem(T *newItem);
  16.         void setLeft(TreeNode *newLeft);
  17.         void setRight(TreeNode *newRight);
  18.         T getItem();
  19.         TreeNode<T>* getLeft();
  20.         TreeNode<T>* getRight();
  21.         bool IsSameNode(T c);
  22.         T item;
  23.  
  24.     private:
  25.         TreeNode *left;
  26.         TreeNode *right;
  27. };
  28.  
  29.  
  30. template <class T>
  31. TreeNode<T>::TreeNode(T newItem)
  32. {
  33.     item = newItem;
  34.     left = null;
  35.     right = null;
  36. }
  37.  
  38. template <class T>
  39. TreeNode<T>::~TreeNode(void)
  40. {
  41. }
  42.  
  43.  
  44. template <class T>
  45. void TreeNode<T>::setItem(T *newItem) 
  46. {
  47.     // set methods
  48.     item = newItem;
  49. }
  50.  
  51. template <class T>
  52. void TreeNode<T>::setLeft(TreeNode *newLeft) 
  53. {
  54.     left = newLeft;
  55. }
  56.  
  57. template <class T>
  58. void TreeNode<T>::setRight(TreeNode *newRight) 
  59. {
  60.     right = newRight;
  61. }
  62.  
  63. template <class T>
  64. T TreeNode<T>::getItem()
  65. {
  66.     // get methods
  67.     return item;
  68. }
  69.  
  70. template <class T>
  71. TreeNode<T>* TreeNode<T>::getLeft() 
  72. {
  73.     return left;
  74. }
  75.  
  76. template <class T>
  77. TreeNode<T>* TreeNode<T>::getRight() 
  78. {
  79.     return right;
  80. }
  81.  
  82. template <class T>
  83. bool TreeNode<T>::IsSameNode(T c)
  84. {
  85.     return this->item == c;
  86. }
  87.  

Member
 
Join Date: Jul 2008
Posts: 34
#2: Sep 6 '08

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


please feel free to ask me any qns or suggest any suggestions.. i am up for anything.. thanks :)
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#3: Sep 6 '08

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


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:
Expand|Select|Wrap|Line Numbers
  1. TreeNode<char>* assign;
  2. assign = searchTree(treeObj->root ,data1);
  3.  
or maybe you meant to assign to be of type char, and to use the TreeNode's item variable:
Expand|Select|Wrap|Line Numbers
  1. char assign;
  2. assign = searchTree(treeObj->root ,data1)->item;
  3.  
Well, I hope this is somewhat helpful. Good luck.
Member
 
Join Date: Jul 2008
Posts: 34
#4: Sep 6 '08

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


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1. TreeNode<char>* assign;
  2. assign = searchTree(treeObj->root ,data1);
  3.  
or maybe you meant to assign to be of type char, and to use the TreeNode's item variable:
Expand|Select|Wrap|Line Numbers
  1. char assign;
  2. assign = searchTree(treeObj->root ,data1)->item;
  3.  
Well, I hope this is somewhat helpful. Good luck.


yup it was cheers! :)
Reply