Невозможно вывести строку из шаблона класса
Я пытался заставить свой шаблон класса распечатать строку для меня. Однако я получаю error C679: No operator found which takes a right hand operand of type std:: string
, Я пробовал различные способы, чтобы эта ошибка исчезла.
#include <iostream>
#include <string.h>
using namespace std;
template<class T>
class BinaryTree
{
struct Node
{
T data;
Node* lChildptr;
Node* rChildptr;
Node(T dataNew)
{
data = dataNew;
lChildptr = NULL;
rChildptr = NULL;
}
};
private:
Node* root;
void Insert(Node* &newData, Node* &theRoot)
{
if(theRoot == NULL)
{
theRoot = new Node(newData);
return;
}
else if(newData->data < theRoot->data)
Insert(newData, theRoot->lChildptr);
else
Insert(newData, theRoot->rChildptr);;
}
void insertnode(T item)
{
Node *newData;
newData= new Node;
newData->value = item;
newData->lChildptr = newData = rChildptr = NULL;
insert ( newData, root);
}
void PrintTree(Node* theRoot)
{
if(theRoot)
{
cout << theRoot->data << endl;
PrintTree(theRoot->lChildptr); //<< The error is here
PrintTree(theRoot->rChildptr);
}
}
public:
BinaryTree()
{
root = NULL;
}
void AddItem(T )
{
//Insert(newData, root);
}
void PrintTree()
{
PrintTree(root);
}
};
int main()
{
BinaryTree<string> *tree = new BinaryTree<string>();
tree->AddItem("Erick");
tree->PrintTree();
system ( "pause");
}
1 ответ
Я изменился void insertnode(T item)
а также void Insert(Node* &newData, Node* &theRoot)
и без комментариев //Insert(newData, root);
здесь фиксированный код...
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
template<class T>
class BinaryTree
{
struct Node
{
T data;
Node* lChildptr;
Node* rChildptr;
Node(T dataNew)
{
data = dataNew;
lChildptr = NULL;
rChildptr = NULL;
}
};
private:
Node* root;
void Insert(Node* newData, Node* &theRoot)
{
if(theRoot == NULL)
{
theRoot = newData;
return;
}
else if(newData->data < theRoot->data)
Insert(newData, theRoot->lChildptr);
else
Insert(newData, theRoot->rChildptr);;
}
void insertnode(T item)
{
Node *newData;
newData= new Node(item);
Insert ( newData, root);
}
void PrintTree(Node* &theRoot)
{
if(theRoot)
{
cout << theRoot->data << endl;
PrintTree(theRoot->lChildptr); //<< The error is here
PrintTree(theRoot->rChildptr);
}
}
public:
BinaryTree()
{
root = NULL;
}
void AddItem(T item)
{
insertnode(item);
}
void PrintTree()
{
PrintTree(root);
}
};
int main()
{
BinaryTree<string> *tree = new BinaryTree<string>();
tree->AddItem("Erick");
tree->PrintTree();
system ( "pause");
}