从模板Graph Node类创建子二叉树节点类

时间:2015-10-22 07:42:35

标签: c++ graph

我有一个Graph类,其中包含以下简单实现:

template <typename T>
class GraphNode {

public:
    T data;
    vector<GraphNode*> adj;

    void Print(GraphNode<T>* node) {

        if(!node) {
            std::cout << "*";
            return;
        }

        std::cout << node->data << ":";

        for(typename vector<GraphNode<T>* >::iterator iter = adj.begin();
                iter != adj.end();
                iter++)
        {
            Print(iter);
        }
    }

};

我想创建&#34;二叉树节点&#34;继承此GraphNode类的类,但我无法弄清楚如何执行此操作。我写了一个不完整的类,但是我收到了几个编译错误。这是代码:

template <typename T>
// Binary Tree Node
class BinaryTreeNode : public GraphNode<T> {

public:
    BinaryTreeNode<T>* lhs;
    BinaryTreeNode<T>* rhs;

    BinaryTreeNode() {
        adj.push_back(NULL);
        adj.push_back(NULL);

        lhs = adj[0];
        rhs = adj[1];
    }

    BinaryTreeNode(T in_data) {
        data = in_data;

        adj.push_back(NULL);
        adj.push_back(NULL);

        lhs = adj[0];
        rhs = adj[1];
    }


    BinaryTreeNode& operator=(const BinaryTreeNode& other) {

        // if the other item is this, then return itself
        if(&other != this) {
            data = other.data;
            // copy the vector
            lhs = other.lhs;
            rhs = other.rhs;
        }
        return *this;
    }

};

错误列表

../src/BinaryTree.h:22:3: error: ‘adj’ was not declared in this scope
../src/BinaryTree.h:30:3: error: ‘data’ was not declared in this scope

1 个答案:

答案 0 :(得分:0)

问题来源描述为here

This is my solution

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class GraphNode {

public:
    T data;
    vector<GraphNode*> adj;

    void Print(GraphNode<T>* node) {

        if(!node) {
            std::cout << "*";
            return;
        }

        std::cout << node->data << ":";

        for(typename vector<GraphNode<T>* >::iterator iter = adj.begin();
                iter != adj.end();
                iter++)
        {
            Print(iter);
        }
    }
};

template <typename T>
class BinaryTreeNode : public GraphNode<T> {
    using GraphNode<T>::data;
    using GraphNode<T>::adj;

public:
    BinaryTreeNode<T>* lhs;
    BinaryTreeNode<T>* rhs;

    BinaryTreeNode() {
        adj.push_back(NULL);
        adj.push_back(NULL);

        lhs = NULL;
        rhs = NULL;
    }

    BinaryTreeNode(T in_data) {
        data = in_data;

        adj.push_back(NULL);
        adj.push_back(NULL);

        lhs = NULL;
        rhs = NULL;
    }


    BinaryTreeNode& operator=(const BinaryTreeNode& other) {

        // if the other item is this, then return itself
        if(&other != this) {
            data = other.data;
            // copy the vector
            lhs = other.lhs;
            rhs = other.rhs;
        }
        return *this;
    }

};

int main() {
    BinaryTreeNode<int> node;
    cout << 42;
    return 0;
}

请注意,您尝试进行隐式向下转换。这是不允许的。您必须使用lhs = (BinaryTreeNode*)adj[0];或其他一些演员(可能是dynamic_cast)。