N 叉树实现:无法在赋值中将 'Tree::node*' 转换为 'node*'
N-ary Tree implementation: cannot convert 'Tree::node*' to 'node*' in assignment
我正在尝试用指针实现的列表列表创建一个 n 叉树,这是我对 Tree
:
的实现
class Tree{
public:
struct nodeSon{
struct node* node_ptr;
nodeSon* nextNodeSon_ptr;
};
struct node{
char label;
node* nextNode_ptr;
struct nodeSon* nSon;
};
node* root;
void AddSon(int i, node n, char l); //Adds a son with label l to n at i position
};
当我尝试执行 Tree::AddSon()
:
时出现问题
void Tree::AddSon(int i, nodo n, char l){
node* nextPtr = root->nextNode_ptr;
node* newNode = new node();
newNode->label= l;
root->nextNode_ptr = newNode;
newNode->nextNode_ptr = nextPtr;
node* it = root;
while (it != &n) {
it = it->nextNode_ptr;
}
nodeSon* it2 = it->nSon;
int counter = 1;
while (contador != i) {
it2 = it2->sigNodoHijo_ptr;
}
nodeSon* nextPtrNH = it2->nextNodeSon_ptr;
nodeSon* newNodeNH = new nodeSon();
newNodeNH->node_ptr = newNodo; //error indication at this line
it2 = newNodeNH;
newNodoNH->nextNodeSon_ptr = nextPtrNH;
.
.
.
}
我在尝试构建它时收到此错误:
Tree.cpp:110:27: error: cannot convert 'Tree::node*' to 'node*' in assignment
- 为什么会这样?
- 我该如何解决这个问题?
问题
class Tree{
public:
struct nodeSon{
struct node* node_ptr; // (1)
nodeSon* nextNodeSon_ptr;
};
};
在 (1)
处,您基本上是在告诉编译器 node_ptr
应指向您尚未定义的类型,但此类型的定义稍后可用。
问题是编译器不能假设这个类型驻留在 class Tree
中——因为你没有说是这种情况——相反它会假设 struct node
指的是一个类型全局 命名空间。
当您稍后在 class Tree
中声明一个名为 node
的类型时,已经太晚了; Tree::nodeSon::node_ptr
的类型不会因为(从我们的角度来看)更合适的类型可用而改变。
例子
我们可以使用以下大大简化的测试用例重现您的错误。
struct A {
struct B {
struct C * ptr;
};
struct C { };
};
struct C { };
int main () {
A::B x;
A::C y;
C z;
x.ptr = &y; // error, `y` is of type `A::c`
x.ptr = &z; // ok, `z` is of type `::C`
}
解决方案
在编写 Tree::nodeSon
的定义之前,您需要通过前向声明告诉编译器在 Tree
中将存在一个名为 node
的类型。
class Tree{
public:
struct node; // forward-declare Tree::node
struct nodeSon{
struct node* node_ptr;
nodeSon* nextNodeSon_ptr;
};
struct node{
char label;
node* nextNode_ptr;
struct nodeSon* nSon;
};
node* root;
void AddSon (int i, node n, char l); //Adds a son with label l to n at i position
};
我正在尝试用指针实现的列表列表创建一个 n 叉树,这是我对 Tree
:
class Tree{
public:
struct nodeSon{
struct node* node_ptr;
nodeSon* nextNodeSon_ptr;
};
struct node{
char label;
node* nextNode_ptr;
struct nodeSon* nSon;
};
node* root;
void AddSon(int i, node n, char l); //Adds a son with label l to n at i position
};
当我尝试执行 Tree::AddSon()
:
void Tree::AddSon(int i, nodo n, char l){
node* nextPtr = root->nextNode_ptr;
node* newNode = new node();
newNode->label= l;
root->nextNode_ptr = newNode;
newNode->nextNode_ptr = nextPtr;
node* it = root;
while (it != &n) {
it = it->nextNode_ptr;
}
nodeSon* it2 = it->nSon;
int counter = 1;
while (contador != i) {
it2 = it2->sigNodoHijo_ptr;
}
nodeSon* nextPtrNH = it2->nextNodeSon_ptr;
nodeSon* newNodeNH = new nodeSon();
newNodeNH->node_ptr = newNodo; //error indication at this line
it2 = newNodeNH;
newNodoNH->nextNodeSon_ptr = nextPtrNH;
.
.
.
}
我在尝试构建它时收到此错误:
Tree.cpp:110:27: error: cannot convert 'Tree::node*' to 'node*' in assignment
- 为什么会这样?
- 我该如何解决这个问题?
问题
class Tree{
public:
struct nodeSon{
struct node* node_ptr; // (1)
nodeSon* nextNodeSon_ptr;
};
};
在 (1)
处,您基本上是在告诉编译器 node_ptr
应指向您尚未定义的类型,但此类型的定义稍后可用。
问题是编译器不能假设这个类型驻留在 class Tree
中——因为你没有说是这种情况——相反它会假设 struct node
指的是一个类型全局 命名空间。
当您稍后在 class Tree
中声明一个名为 node
的类型时,已经太晚了; Tree::nodeSon::node_ptr
的类型不会因为(从我们的角度来看)更合适的类型可用而改变。
例子
我们可以使用以下大大简化的测试用例重现您的错误。
struct A {
struct B {
struct C * ptr;
};
struct C { };
};
struct C { };
int main () {
A::B x;
A::C y;
C z;
x.ptr = &y; // error, `y` is of type `A::c`
x.ptr = &z; // ok, `z` is of type `::C`
}
解决方案
在编写 Tree::nodeSon
的定义之前,您需要通过前向声明告诉编译器在 Tree
中将存在一个名为 node
的类型。
class Tree{
public:
struct node; // forward-declare Tree::node
struct nodeSon{
struct node* node_ptr;
nodeSon* nextNodeSon_ptr;
};
struct node{
char label;
node* nextNode_ptr;
struct nodeSon* nSon;
};
node* root;
void AddSon (int i, node n, char l); //Adds a son with label l to n at i position
};