【发布时间】: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
【问题讨论】:
-
虽然,其实我觉得stackoverflow.com/q/32665178/995218的解释更好。