Chinaunix首页 | 论坛 | 博客
  • 博客访问: 712505
  • 博文数量: 98
  • 博客积分: 3257
  • 博客等级: 中校
  • 技术积分: 966
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-15 17:19
文章存档

2020年(1)

2018年(1)

2017年(1)

2016年(2)

2015年(2)

2013年(3)

2012年(24)

2011年(28)

2010年(4)

2009年(9)

2008年(23)

我的朋友

分类: C/C++

2012-03-03 22:09:36

c++中编写二叉树代码时,可构造二叉树类consTree继承一般二叉树类binaryTree,一般二叉树类中有个Protected成员变量node * root;在可构造二叉树的构造函数和其他成员函数中调用它时,老是报错,说找不到该成员的定义,理论上来说,子类应该可以使用父类中的protected成员。结果发现,直接用root老报错,应该是用this->root的方式调用就成功。部分主要源代码如下:
//基本二叉树类的定义
template
class binaryTree {
    public:
        binaryTree();
        binaryTree(const binaryTree &bt);
         ~binaryTree();
        //其他成员函数
        binaryTree * copy() const;
        void deleteAllValues();
        int isEmpty();
        node * getroot() const;
    protected:
        node* root; //树根
        friend class Tree;
        friend class treeTravel; //遍历器友类
};
//可构造二叉树类的定义
template
class consTree:public binaryTree {
    public:
        //构造函数
        consTree();
        consTree(const T value);
        consTree(const T val,consTree left,consTree right);
        //成员函数
        consTree * left(); //取复制出的左子树
        consTree * right(); //取右子树
        void left(consTree *l); //替换、设置左子树
        void right(consTree *r);
};
//构造函数调用时
template
consTree::consTree(const T value) {
    this->root=new node(value);
}
就在这里,如果不加this指针编译器报错,编译环境为netbeans7.1 + minGW(G++ 4.6.2)。
 
阅读(15928) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

Windy_haha2014-11-25 09:25:47

超级有用。。。。专门注册了账号过来点赞。。。。