奇怪的错误

piaomiao163 2009-06-07 11:36:04
#include <iostream>
using namespace std;

struct treeNod;

struct headTreeNod
{
int length;
treeNod *next;
};

struct treeNod
{
treeNod *lchild, *rchild;
};

class tree
{
public:
tree();
void creatTree( treeNod *pTmp );
private:
headTreeNod *pheadNod;
};

tree::tree()
{
pheadNod->length = 0;
//pheadNod->next = NULL;
}

void tree::creatTree( treeNod *pTmp )
{
int i;
cout << "请输入结点标识符" << endl;
cin >> i;
if( i == 0 )
pTmp = NULL;
else
{
if( pheadNod->next == NULL )
{
pheadNod->next = pTmp;
}

this->creatTree( pTmp->lchild );
this->creatTree( pTmp->rchild );
}
}

int main()
{
tree tree1;
treeNod *pTmp;

//tree1.creatTree( pTmp );
return 0;
}




在初始化一个对象的时候 pheadNod->length = 0; 这一步出错了·~~~


···郁闷·~帮忙看看为什么??语法没错误!!



...全文
27 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2009-06-07
  • 打赏
  • 举报
回复
逻辑很乱.struct headTreeNod
{
int length;
treeNod *next;
};

struct treeNod
{
treeNod *lchild, *rchild;
};为什么不合成一个类呢?
adventurelw 2009-06-07
  • 打赏
  • 举报
回复
tree::tree()
{
pheadNod->length = 0;
//pheadNod->next = NULL;
}

pheadNod并没有明确指向,当然不能初始化啊。。。。。。通常,这种情况都需要动态分配的。
piaomiao163 2009-06-07
  • 打赏
  • 举报
回复
失误失误!!!!!!!!!!!惭愧!!!!!!!!!
pengzhixi 2009-06-07
  • 打赏
  • 举报
回复
tree::tree()
{
pheadNod->length = 0; //你这个pheadNod都没分配内存,你就使用其中的数据
//pheadNod->next = NULL;
}
光宇广贞 2009-06-07
  • 打赏
  • 举报
回复
private:
headTreeNod *pheadNod;
};

tree::tree()
{
pheadNod->length = 0;
//pheadNod->next = NULL;
}

有错,只可能你这个 pheadNod 没有给丫申请空间,丫是一个野指针。
liao05050075 2009-06-07
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

struct treeNod;

struct headTreeNod
{
int length;
treeNod *next;
};

struct treeNod
{
treeNod *lchild, *rchild;
};

class tree
{
public:
tree();
void creatTree( treeNod *pTmp );
private:
headTreeNod *pheadNod;
};

tree::tree()
{
//你还没有为pheadNod申请空间就要去改它的内容,当然会错啦,加个new就行了
pheadNod=new headTreeNod;
pheadNod->length = 0;
//pheadNod->next = NULL;
}

void tree::creatTree( treeNod *pTmp )
{
int i;
cout << "请输入结点标识符" << endl;
cin >> i;
if( i == 0 )
pTmp = NULL;
else
{
if( pheadNod->next == NULL )
{
pheadNod->next = pTmp;
}

this->creatTree( pTmp->lchild );
this->creatTree( pTmp->rchild );
}
}

int main()
{
tree tree1;
treeNod *pTmp;

//tree1.creatTree( pTmp );
return 0;
}

65,211

社区成员

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

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