【问题标题】:Creating a Child Binary Tree Node class from a template Graph Node class [duplicate]从模板图节点类创建子二叉树节点类[重复]
【发布时间】:2015-10-22 07:42:35
【问题描述】:

我有一个 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);
        }
    }

};

我想创建继承此 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

【问题讨论】:

标签: c++ graph


【解决方案1】:

您的问题根源描述为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)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多