0

I have a Graph class which the following simple implementation:

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);
        }
    }

};

And I would like to create "Binary Tree Node" class which inherits this GraphNode class but I am having trouble figuring out how to do this. I've written an incomplete class but I am getting several compile error. Here is the code:

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;
    }

};

List of errors

../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
ArmenB
  • 2,125
  • 3
  • 23
  • 47
  • Although, actually, I think the explanation at http://stackoverflow.com/q/32665178/995218 is better. – atkins Oct 22 '15 at 08:18

1 Answers1

0

The source of your problem is described 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;
}

Note that you were trying to do an implicit downcast. This is not allowed. You either have to use lhs = (BinaryTreeNode*)adj[0]; or some other cast (probably dynamic_cast).