二叉树 语法问题..

huang_555 2011-06-18 11:51:27

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class BTnode;

template <typename Type>
class BinaryTree
{
public:
BinaryTree();
BinaryTree(const BinaryTree &a);
~BinaryTree();
BinaryTree& operator=(const BinaryTree &a);

bool empty() const{return _root==0;}
bool clear();
void insert(const BTnode<Type> &a);
void remove(const Type &a);//删除 叶子 a
void remove_root();//删除 根节点
void clear()//删除全部
{
if (_root)
{
clear(_root);//private版的clear()
_root = 0;//防止这家伙乱来
}
return;
}

protected:
private:
BTnode<Type> *_root;
void clear(BTnode<Type> *bt); //删除整棵树
//把src所指的值复制到tar上去
void copy(BinaryTree<Type> *tar,BinaryTree<Type> *src);
};
template <typename Type>
inline BinaryTree<Type>::BinaryTree():_root(0){}

template <typename Type>
inline BinaryTree<Type>::BinaryTree(const BinaryTree<Type> &a)
{
copy(_root,a._root);
}
template <typename Type>
inline BinaryTree<Type>::~BinaryTree()
{
clear();
}
template <typename Type>
inline BinaryTree& BinaryTree<Type>::operator =(const BinaryTree &a)
{
if (this != &a)
{
clear();
copy(_root,a._root);
}
return *this;

}

template <typename Type>
inline void BinaryTree::insert(const BTnode<Type> &a)
{
if ( ! _root)
{
_root = new BTnode<Type>(a);
}
else
{
_root->insert_val(a);
}
return;
}
template <typename Type>
inline void BinaryTree::remove(const Type &a)
{
if (_root)
{
if (_root->_val == a)
{
remove_root();//根节点 特殊 处理
}
else
{
_root->remove_val(a,_root);

}
}
return;
}
template <typename Type>
inline void BinaryTree::remove_root()
{
if ( ! _root)
{
return;
}

BTnode<Type> *temp = _root;
if (_root->_rchild)
{
_root = _root->_rchild;
//如果 根节点(右子树来的) 没有左子树就直接复制过去,
//如果有 就app在左子树的底部
if (temp->_lchild)
{
BTnode<Type> *lc = temp->_lchild;
BTnode<Type> *newlc = _root->_lchild;
if (newlc)//左子树存在 
{
BTnode<Type>::lchild_left(lc, newlc);
}
else
{
newlc = lc;//不存在 直接复制
}
}
}
else
{
_root = _root->_lchild;
}

delete temp;//最后 释放


}
template <typename Type>//这个也要单步一次..
void BinaryTree::clear(BTnode<Type> *bt)
{
if (bt)
{
clear(bt->_lchild);
clear(bt->_rchild);
delete bt;
}
return;
}
template <typename valtype>
class BTnode //节点
{
friend class BinaryTree<valtype>;
public:
BTnode(const BTnode &a);
void insert_val(const valtype &a);//插入
static void lchild_left(BTnode *left,BTnode *subtree);
void remove_val(const valtype &val, BTnode *&ptr);//从prt节点开始找val这个数值 在删除

protected:
private:
valtype _val;
int _cnt;
BTnode *_lchild;
BTnode *_rchild;

};

template <typename valtype>
inline BTnode::BTnode(const valtype &a):_val(a)
{
_cnt = 1;
_lchild = _rchild = 0;

};

template <typename valtype>
inline void BTnode::insert_val(const valtype &a)
{
if (a == _val)
{
_cnt++;
return;
}
if (a < _val)
{
if (! _lchild)
{
_lchild = new BTnode<Type>(a);
}
else
{
_lchild->insert_val(a);
}
}
else
{
if (! _rchild)
{
_rchild = new BTnode<Type>(a);
}
else
{
_rchild->insert_val(a);
}
}
return;
}
template <typename valtype>
inline void lchild_left(BTnode<valtype> *left, BTnode<valtype> *subtree)
{
while(subtree->_lchild)
{
subtree = subtree->_lchild;
}
subtree->_lchild = left;
return;
}

template <typename valtype>//这个很难多理解
inline void BTnode::remove_val(const valtype &val, BTnode *&ptr)
{
if (val < _val)
{
if ( ! _lchild)
{
return;//左子树没有这值
}
else
{
_lchild->remove_val(val,_lchild);
}
}
else
{
if (val > _val)
{
if ( ! _rchild)
{
return;//右子树没有这值
}
else
{
_rchild->remove_val(val,_lchild);
}
}
else
{
//不负重望,终于找到了..
if (_rchild)
{
ptr = _rchild;//节点变成 节点的右子树
if ( ! ptr->_lchild)
{
ptr->_lchild = _lchild;//这句很深要多理解
}
else
{
BTnode<valtype>::lchild_left(_lchild,ptr->_lchild);//把左子树add在右子树的左节点的底部
}
}
else
{
ptr = _lchild;//节点变成 节点的左子树
}
delete this;//最后释放
}

}

return;
}


错误如下:
------ 已启动生成: 项目: templet模板化, 配置: Debug Win32 ------
正在编译...
1.cpp
d:\程序..\templet模板化\templet模板化\1.cpp(19) : error C2143: 语法错误 : 缺少“)”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(38): 参见对正在编译的类 模板 实例化“BinaryTree<Type>”的引用
d:\程序..\templet模板化\templet模板化\1.cpp(19) : error C2143: 语法错误 : 缺少“;”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(19) : error C2059: 语法错误 : “)”
d:\程序..\templet模板化\templet模板化\1.cpp(19) : error C2238: 意外的标记位于“;”之前
d:\程序..\templet模板化\templet模板化\1.cpp(34) : error C2059: 语法错误 : “<”
d:\程序..\templet模板化\templet模板化\1.cpp(34) : error C2238: 意外的标记位于“;”之前
d:\程序..\templet模板化\templet模板化\1.cpp(35) : error C2143: 语法错误 : 缺少“)”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(35) : error C2143: 语法错误 : 缺少“;”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(35) : error C2059: 语法错误 : “)”
d:\程序..\templet模板化\templet模板化\1.cpp(35) : error C2238: 意外的标记位于“;”之前
d:\程序..\templet模板化\templet模板化\1.cpp(62) : error C2244: “BinaryTree<Type>::operator =”: 无法将函数定义与现有的声明匹配
d:\程序..\templet模板化\templet模板化\1.cpp(15) : 参见“BinaryTree<Type>::operator =”的声明
定义
'BinaryTree &BinaryTree<Type>::operator =(const BinaryTree<Type> &)'
现有声明
'BinaryTree<Type> &BinaryTree<Type>::operator =(const BinaryTree<Type> &)'
d:\程序..\templet模板化\templet模板化\1.cpp(65) : error C2143: 语法错误 : 缺少“)”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(65) : error C2143: 语法错误 : 缺少“;”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(65) : error C2244: “BinaryTree<Type>::insert”: 无法将函数定义与现有的声明匹配
d:\程序..\templet模板化\templet模板化\1.cpp(19) : 参见“BinaryTree<Type>::insert”的声明
定义
'void BinaryTree::insert(const BTnode)'
现有声明
'void BinaryTree<Type>::insert(const BTnode)'
d:\程序..\templet模板化\templet模板化\1.cpp(65) : error C2988: 不可识别的模板声明/定义
d:\程序..\templet模板化\templet模板化\1.cpp(65) : error C2059: 语法错误 : “<”
d:\程序..\templet模板化\templet模板化\1.cpp(65) : error C2059: 语法错误 : “)”
d:\程序..\templet模板化\templet模板化\1.cpp(79) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(79) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
d:\程序..\templet模板化\templet模板化\1.cpp(130) : error C2244: “BinaryTree<Type>::remove_root”: 无法将函数定义与现有的声明匹配
d:\程序..\templet模板化\templet模板化\1.cpp(21) : 参见“BinaryTree<Type>::remove_root”的声明
定义
'void BinaryTree::remove_root(void)'
现有声明
'void BinaryTree<Type>::remove_root(void)'
d:\程序..\templet模板化\templet模板化\1.cpp(132) : warning C4346: “BinaryTree<Type>::clear”: 依赖名称不是类型
用“typename”为前缀来表示类型
d:\程序..\templet模板化\templet模板化\1.cpp(132) : error C2143: 语法错误 : 缺少“)”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(132) : error C2143: 语法错误 : 缺少“;”(在“<”的前面)
d:\程序..\templet模板化\templet模板化\1.cpp(132) : error C2244: “BinaryTree<Type>::clear”: 无法将函数定义与现有的声明匹配
定义
'void BinaryTree::clear(BTnode)'
现有声明
'void BinaryTree<Type>::clear(BTnode)'
'void BinaryTree<Type>::clear(void)'
'bool BinaryTree<Type>::clear(void)'
d:\程序..\templet模板化\templet模板化\1.cpp(132) : error C2988: 不可识别的模板声明/定义
d:\程序..\templet模板化\templet模板化\1.cpp(132) : error C2059: 语法错误 : “<”
d:\程序..\templet模板化\templet模板化\1.cpp(132) : error C2059: 语法错误 : “)”
d:\程序..\templet模板化\templet模板化\1.cpp(159) : error C2989: “BTnode”: 类 模板 已经声明为非类 模板
d:\程序..\templet模板化\templet模板化\1.cpp(6) : 参见“BTnode”的声明
d:\程序..\templet模板化\templet模板化\1.cpp(162) : error C2039: “{ctor}”: 不是“BTnode”的成员
d:\程序..\templet模板化\templet模板化\1.cpp(144) : 参见“BTnode”的声明
d:\程序..\templet模板化\templet模板化\1.cpp(162) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
d:\程序..\templet模板化\templet模板化\1.cpp(162) : error C2904: “BTnode”: 名称已经用于当前范围内的模板
d:\程序..\templet模板化\templet模板化\1.cpp(144) : 参见“BTnode”的声明
d:\程序..\templet模板化\templet模板化\1.cpp(170) : error C2039: “insert_val”: 不是“BTnode”的成员
d:\程序..\templet模板化\templet模板化\1.cpp(144) : 参见“BTnode”的声明
d:\程序..\templet模板化\templet模板化\1.cpp(202) : error C2296: “*”: 非法,左操作数包含“int (__cdecl *)(const valtype &)”类型
d:\程序..\templet模板化\templet模板化\1.cpp(202) : error C2297: “*”: 非法,右操作数包含“std::ios_base &(__cdecl *)(std::ios_base &)”类型
d:\程序..\templet模板化\templet模板化\1.cpp(202) : error C2065: “subtree”: 未声明的标识符
d:\程序..\templet模板化\templet模板化\1.cpp(210) : error C2182: “lchild_left”: 非法使用“void”类型
d:\程序..\templet模板化\templet模板化\1.cpp(210) : error C2433: “lchild_left”: 不允许在数据声明中使用“inline”
d:\程序..\templet模板化\templet模板化\1.cpp(210) : error C2998: “int lchild_left”: 不能是模板定义
d:\程序..\templet模板化\templet模板化\1.cpp(213) : error C2039: “remove_val”: 不是“BTnode”的成员
d:\程序..\templet模板化\templet模板化\1.cpp(144) : 参见“BTnode”的声明
d:\程序..\templet模板化\templet模板化\1.cpp(213) : error C2061: 语法错误 : 标识符“BTnode”
生成日志保存在“file://d:\程序..\templet模板化\templet模板化\Debug\BuildLog.htm”
templet模板化 - 39 个错误,1 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

看到这么多的错误就头疼,,,,,高手帮帮忙,看下....
小弟深夜等....

最后想问一下...
BTnode<Type> 有什么区别....  
BTnode

为什么我上面的源码中有时用这有时用那,真得给搞云了,新手问下...

...全文
120 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
huang_555 2011-06-19
  • 打赏
  • 举报
回复
D下吗????急呀....
huang_555 2011-06-19
  • 打赏
  • 举报
回复
楼上大哥,你是怎么改的,新手 太 JD 了
谢谢..

你能在半夜抽空帮肋我,我表示很感激,

由于我的再生父母,谢谢...

真的很想谢谢你,能留个电话吗...

谢 谢...
booxiong 2011-06-19
  • 打赏
  • 举报
回复
已经改好。楼主记住几点
1:前向申明模板类时,需要加上template <typename T>
2: 在外面定义函数时,需要在类域前面加上<T>,即使是静态函数也不能放过
3: Node的构造函数声明和定义不一致。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename valtype>
class BTnode;

template <typename Type>
class BinaryTree
{
public:
BinaryTree();
BinaryTree(const BinaryTree &a);
~BinaryTree();
BinaryTree& operator=(const BinaryTree &a);

bool empty() const{return _root==0;}
bool clear();
void insert(const BTnode<Type> &a);
void remove(const Type &a);//删除 叶子 a
void remove_root();//删除 根节点
void clear()//删除全部
{
if (_root)
{
clear(_root);//private版的clear()
_root = 0;//防止这家伙乱来
}
return;
}

protected:
private:
BTnode<Type> *_root;
void clear(BTnode<Type> *bt); //删除整棵树
//把src所指的值复制到tar上去
void copy(BinaryTree<Type> *tar,BinaryTree<Type> *src);
};
template <typename Type>
inline BinaryTree<Type>::BinaryTree():_root(0){}

template <typename Type>
inline BinaryTree<Type>::BinaryTree(const BinaryTree<Type> &a)
{
copy(_root,a._root);
}
template <typename Type>
inline BinaryTree<Type>::~BinaryTree()
{
clear();
}
template <typename Type>
inline BinaryTree<Type>& BinaryTree<Type>::operator =(const BinaryTree &a)
{
if (this != &a)
{
clear();
copy(_root,a._root);
}
return *this;

}

template <typename Type>
inline void BinaryTree<Type>::insert(const BTnode<Type> &a)
{
if ( ! _root)
{
_root = new BTnode<Type>(a);
}
else
{
_root->insert_val(a);
}
return;
}
template <typename Type>
inline void BinaryTree<Type>::remove(const Type &a)
{
if (_root)
{
if (_root->_val == a)
{
remove_root();//根节点 特殊 处理
}
else
{
_root->remove_val(a,_root);

}
}
return;
}
template <typename Type>
inline void BinaryTree<Type>::remove_root()
{
if ( ! _root)
{
return;
}

BTnode<Type> *temp = _root;
if (_root->_rchild)
{
_root = _root->_rchild;
//如果 根节点(右子树来的) 没有左子树就直接复制过去,
//如果有 就app在左子树的底部
if (temp->_lchild)
{
BTnode<Type> *lc = temp->_lchild;
BTnode<Type> *newlc = _root->_lchild;
if (newlc)//左子树存在 
{
BTnode<Type>::lchild_left(lc, newlc);
}
else
{
newlc = lc;//不存在 直接复制
}
}
}
else
{
_root = _root->_lchild;
}

delete temp;//最后 释放


}
template <typename Type>//这个也要单步一次..
void BinaryTree<Type>::clear(BTnode<Type> *bt)
{
if (bt)
{
clear(bt->_lchild);
clear(bt->_rchild);
delete bt;
}
return;
}

template <typename valtype>
class BTnode //节点
{
friend class BinaryTree<valtype>;
public:
BTnode(const valtype &a);
void insert_val(const valtype &a);//插入
static void lchild_left(BTnode *left,BTnode *subtree);
void remove_val(const valtype &val, BTnode *&ptr);//从prt节点开始找val这个数值 在删除

protected:
private:
valtype _val;
int _cnt;
BTnode *_lchild;
BTnode *_rchild;

};

template <typename valtype>
inline BTnode<valtype>::BTnode(const valtype &a):_val(a)
{
_cnt = 1;
_lchild = _rchild = 0;

};

template <class valtype>
inline void BTnode<valtype>::insert_val(const valtype &a)
{
if (a == _val)
{
_cnt++;
return;
}
if (a < _val)
{
if (! _lchild)
{
_lchild = new BTnode<Type>(a);
}
else
{
_lchild->insert_val(a);
}
}
else
{
if (! _rchild)
{
_rchild = new BTnode<Type>(a);
}
else
{
_rchild->insert_val(a);
}
}
return;
}
template <typename valtype>
inline void BTnode<valtype>::lchild_left(BTnode<valtype> *left, BTnode<valtype> *subtree)
{
while(subtree->_lchild)
{
subtree = subtree->_lchild;
}
subtree->_lchild = left;
return;
}

template <typename valtype>//这个很难多理解
inline void BTnode<valtype>::remove_val(const valtype &val, BTnode *&ptr)
{
if (val < _val)
{
if ( ! _lchild)
{
return;//左子树没有这值
}
else
{
_lchild->remove_val(val,_lchild);
}
}
else
{
if (val > _val)
{
if ( ! _rchild)
{
return;//右子树没有这值
}
else
{
_rchild->remove_val(val,_lchild);
}
}
else
{
//不负重望,终于找到了..
if (_rchild)
{
ptr = _rchild;//节点变成 节点的右子树
if ( ! ptr->_lchild)
{
ptr->_lchild = _lchild;//这句很深要多理解
}
else
{
BTnode<valtype>::lchild_left(_lchild,ptr->_lchild);//把左子树add在右子树的左节点的底部
}
}
else
{
ptr = _lchild;//节点变成 节点的左子树
}
delete this;//最后释放
}

}

return;
}

64,649

社区成员

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

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