继承模板类以创建具有嵌套类继承的模板类

时间:2019-01-10 18:44:59

标签: c++ templates inheritance inner-classes

我正在尝试为不同的二叉树创建模板类。尝试继承和使用嵌套类时遇到问题。

下面是基类

BinaryTree.h

#include <iostream>
#include "exception.h"
#include "stack.h"
template <class T>
class BinaryTree
{
protected:
    class BinaryTreeNode {
    public:
        T value;
        BinaryTreeNode* left;
        BinaryTreeNode* right;
        BinaryTreeNode(T value);
        ~BinaryTreeNode();
    };
    int count;
    BinaryTreeNode* head;
    void in_order_print(BinaryTreeNode *print_node) const;
    virtual bool delete_node(BinaryTreeNode** temp, T value);
    void delete_node(BinaryTreeNode* temp);
public:
    BinaryTree();
    ~BinaryTree();
    virtual void insert_node(T value);
    void delete_node(T value);
    void in_order_print() const;
};


 //definition of other functions are removed removed to reduce the question size

template<class T>
void BinaryTree<T>::insert_node(T value)
{
    count++;
    int temp;
    BinaryTreeNode* new_node = new BinaryTreeNode(value);
    Stack<bool> travers_path;
    BinaryTreeNode** insert_point = &head;
    temp = count;
    while (temp>0)
    {
        travers_path.push(temp & 1);
        temp >>= 1;
    }
    std::cout << travers_path;
    travers_path.pop();
    try 
    {
        while (true)
        {
            if (travers_path.pop() == false)
                insert_point = &((*insert_point)->left);
            else
                insert_point = &((*insert_point)->right);
        }
    }
    catch (EXCEPTION* e)
    {
        std::cout << e;
    }
    *insert_point = new_node;
    in_order_print();
}

main.cpp

#include "BinaryTree.h"
#include <iostream>
using namespace std; 
int main(){
    BinaryTree<int> x;
    int nodes[] = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
    int count = sizeof(nodes)/sizeof(int);
    for (int i = 0; i < count; i++)
        x.insert_node(nodes[i]);
    int delete_nodes[] = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
    count = sizeof(delete_nodes) / sizeof(int);
    for (int i = 0; i < count; i++)
        x.delete_node(nodes[i]);
}

上面的代码有效,我正在尝试以下操作

BinarySearchTree.h

#include"BinaryTree.h"

template <class T>
class BinarySearchTree :public BinaryTree<T>
{
protected:
    class BinarySearchTreeNode:public BinaryTree<T>::BinaryTreeNode{
        int balance_score;

    };
public:
    BinarySearchTree();
    ~BinarySearchTree();
    void insert_node(T value);
};

template<class T>
void BinarySearchTree<T>::insert_node(T value)
{
    this->count++;
    int temp;

// error C2760: syntax error: unexpected token 'identifier', expected ')'
    BinarySearchTreeNode* node = new BinarySearchTreeNode(T value); 


// error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
    BinaryTreeNode* new_node = new BinaryTreeNode(value); 


//  error C7510: 'BinaryTreeNode': use of dependent type name must be prefixed with 'typename'
    BinaryTree<T>::BinaryTreeNode* new_node = new BinaryTree<T>::BinaryTreeNode(value); 


//  error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
(this->BinaryTreeNode)* new_node = new (this->BinaryTreeNode)(value); 

   //error C2760: syntax error: unexpected token 'this', expected 'type specifier'
this->BinaryTreeNode* new_node = new this->BinaryTreeNode(value);
 }

如何正确继承和访问嵌套类??

  // error C2760: syntax error: unexpected token 'identifier', expected ')'
    BinarySearchTreeNode* node = new BinarySearchTreeNode(T value); 

// error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
    BinaryTreeNode* new_node = new BinaryTreeNode(value); 


//  error C7510: 'BinaryTreeNode': use of dependent type name must be prefixed with 'typename'
    BinaryTree<T>::BinaryTreeNode* new_node = new BinaryTree<T>::BinaryTreeNode(value); 


//  error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
(this->BinaryTreeNode)* new_node = new (this->BinaryTreeNode)(value);

//error C2760: syntax error: unexpected token 'this', expected 'type specifier'
this->BinaryTreeNode* new_node = new this->BinaryTreeNode(value);

编辑: Why do I have to access template base class members through the this pointer?解释了thisthis->count的使用,但在以上情况下this也不起作用

0 个答案:

没有答案