Connecting Tech Pros Worldwide Forums | Help | Site Map

help needed with: error with constructor c++

Member
 
Join Date: Jul 2008
Posts: 34
#1: Sep 6 '08
hi guys,
i get the following error when i try to create a new TreeNode

Expand|Select|Wrap|Line Numbers
  1.  error C2514: 'TreeNode' : class has no constructors
  2.  
this error is caused by the following line:

Expand|Select|Wrap|Line Numbers
  1. treeObj->root = new TreeNode(oneline.c_str());
  2.  
and i have declared the following as a global variable:
Expand|Select|Wrap|Line Numbers
  1. TreeNode<char> *treeNodeObj = NULL;
  2.  
i am very puzzled abt this because my constructor has already been declared...
i have attached the code for the TreeNode.h below:

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.  
any help or suggestion is appreciated so please leave a comment below and i will reply within 5 mins cos i will check this forum every 5mins..
thanks :)

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Sep 6 '08

re: help needed with: error with constructor c++


Unless I'm totally blind, you have a ctor for a char but not for a char*.

kind regards,

Jos
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#3: Sep 6 '08

re: help needed with: error with constructor c++


The string::c_str() returns a const char*.

Your root would have to be:
Expand|Select|Wrap|Line Numbers
  1. TreeNode<const char*> root(str.c_str());
  2.  
for the code to compile and link.
Reply