无法从类模板输出字符串
Can not output a string from a class template
我一直在尝试让我的类模板为我打印出一个字符串。但是我得到了一个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");
}
我更改了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");
}
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何从void函数输出字符串
- 输出字符串数组
- 如何输出字符串第二个单词的第一个字母?
- 多线程环境中的输出字符串流出现问题
- 输出字符串到cmd显示的右显示,但未被认为是命令
- 读取文本文件并输出字符串
- 输出字符串使用 C++ 覆盖 Linux 终端上的最后一个字符串
- 如何在C++中输出字符串的向量
- 无法从类模板输出字符串
- FWRITE 输出字符串不正确
- Qt单元测试输出字符串长度截止
- 无法显示输出字符串
- 如果条目大于90,则输出字符串的布尔函数
- C++不带 'n' 或 endl 的输出字符串将在字符串末尾输出'#'
- 在C++中输出字符串和 strncpy 时遇到问题
- 获取c#输出字符串参数值为c++ BSTR*
- C++初学者,开关不输出字符串的第一个字符
- 输出字符串/字符错误(c++)
- Getline()省略输出字符串的第一个字母