请教...

SDFsd24 2010-09-21 02:33:47
#include <iostream>

using namespace std;

template <typename T>
class tnode
{
public:
T nodeValue;
tnode<T> *left, *right;

tnode() {}
tnode(const T& item,tnode<T> *lptr = NULL,tnode<T> *rptr = NULL) {}
};

template <typename T>
void inorderOutput(tnode<T> *t,const string& separator = " ")
{
if(t != NULL)
{
inorderOutput<T>(t->left, separator);
cout << t->nodeValue << separator;
inorderOutput<T>(t->right, separator);
}
}

int main()
{
tnode<int> *p,*q,*r;
p = new tnode<int>(8);
q = new tnode<int>(20);
r = new tnode<int>(1,p,q);
inorderOutput<int>(r,"haha");
}
请问下这里哪里出错了,还有采用默认的该怎么弄...

...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
SDFsd24 2010-09-21
  • 打赏
  • 举报
回复
写蒙了...
小楫轻舟 2010-09-21
  • 打赏
  • 举报
回复

#include <iostream>
#include <string> // --------------------------加这个

using namespace std;

template <typename T>
class tnode
{
public:
T nodeValue;
tnode<T> *left, *right;

tnode() {left = NULL; right = NULL;} // --------------------left,right初始化
tnode(const T& item,tnode<T> *lptr = NULL,tnode<T> *rptr = NULL)
{ // --------------------nodeValue,left,right初始化
nodeValue = item;
left = NULL;
right = NULL;
}
};

template <typename T>
void inorderOutput(tnode<T> *t,const string& separator = " ")
{
if(t != NULL)
{
inorderOutput<T>(t->left, separator);
cout << t->nodeValue << separator;
inorderOutput<T>(t->right, separator);
}
}

int main()
{
tnode<int> *p,*q,*r = NULL;
p = new tnode<int>(8);
q = new tnode<int>(20);
r = new tnode<int>(1,p,q);
inorderOutput<int>(r,"haha");
}
dxms8 2010-09-21
  • 打赏
  • 举报
回复
r = new tnode<int>(1,p,q);
这个1不行的吧,是不是需要一个引用?

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧