【发布时间】:2019-06-07 08:29:06
【问题描述】:
我需要实现 3 个函数(addElem、member 和 findPathCost),它们在树上操作,其中包含指向具有链表的子节点的指针列表。 struct treeNode 定义树中的节点,childrenListElem 定义treeNode 的子节点列表。
struct tree::treeNode {
Label label;
Weight weight;
childrenList children; //pointer to the list of its children
};
struct tree::childrenListElem {
treeNode* child; //pointer to the first element of the children's list
childrenListElem* next; //pointer to the next one
};
在标题中:
struct treeNode; // forward declaration
typedef treeNode* Tree; // pointer to root of tree
const Tree emptyTree = NULL; // empty tree
struct childrenListElem; // forward declaration
typedef childrenListElem* childrenList;
const childrenList emptyChildrenList = NULL; // empty children list
我的问题是我无法从 treeNode 结构中访问子列表,这是我制作的 addAlemen 和成员的辅助函数中的一个示例:
//AUXILIARY FUNCTION: getNode(Label, Tree) returns the node with the given label in the tree.
//Used both in addElem and in member.
Tree getNode(Label & l, const Tree t)
{
Tree aux = t;
while (!isEmpty(aux)) {
if (aux->label == l)
return aux;
aux = (aux->children)->next; //HERE IS MY PROBLEM:
//usually I would have just done
//aux = aux->NextVertex
//(with NextVertex being the next
//treeNode in the tree t);
//but I can't seem to access the
//second struct as the compiler
//tells me that "children" is
//apparently not a pointer.
//How can I access the second struct?
}
return emptyTree;
}
这是我的编译器显示的错误:
The error is: "error: cannot convert 'tree::childrenListElem*' to 'tree::Tree' {aka 'tree::treeNode*'} in assignment aux = aux->children->next;"
当我改为使用 aux = aux->children.next;我看到了这个错误(我正在使用 gcc 编译器):
The error is: "error: request for member 'next' in 'aux->tree::treeNode::children', which is of pointere type 'tree::childrenList' {aka 'tree::childrenListElem*'} (maybe you meant to use '->'?)
【问题讨论】:
-
请尝试创建一个合适的minimal reproducible example 向我们展示。
Tree是什么? -
childrenList是如何定义的?真的和childrenListElem一样吗? -
也许解决方案就像
aux->children.next一样简单? -
不幸的是它不是!我也试过(aux.children)->下一步,什么都没有!
-
请发布错误。
标签: c++ pointers struct linked-list