错误:“孩子"字段的类型“节点[2]"不完整 [英] error: field ‘children’ has incomplete type ‘Node [2]

查看:107
本文介绍了错误:“孩子"字段的类型“节点[2]"不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时,出现错误:字段'children'具有不完整的类型'Node [0]'"".我正在用C ++编写代码,我想创建一个Node类,该类本身会创建另外两个Node对象,依此类推,直到到达maxDepth为止.我得到的完整错误:

When I run this code, i get the error: "Field 'children' has incomplete type 'Node[0]'". I'm coding in C++ and I want to create a Node class which creates in itself two other Node objects and so on, until it reaches the maxDepth. The full error I get:

18:24:16 **** Incremental Build of configuration Debug for project Tests ****
make all 
Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -std=c++0x -O3 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp:22:17: error: field ‘children’ has incomplete type ‘Node [2]’
  Node children[2];
                 ^
../main.cpp:8:7: note: definition of ‘class Node’ is not complete until the closing brace
 class Node {
       ^
make: *** [main.o] Error 1
subdir.mk:18: recipe for target 'main.o' failed

和代码:

#include <iostream>


using namespace std;



class Node {
public:
    int depth;
    bool value;

    Node(bool value, int depth, int maxDepth) {
        this->value = value;
        this->depth = depth;
        if (this->depth < maxDepth) {
            children[2] = {Node(false, this->depth + 1, maxDepth), Node(true, this->depth + 1, maxDepth)};
        }
    }

private:
    Node children[2];
};


int main() {

    Node tree(false, 0, 1);


    return 0;
}

推荐答案

您已定义类Node来包含Node对象的数组:

You have defined class Node to contain an array of Node objects:

private:
    Node children[2];

一个Node不能包含另一个Node,因为它的固定大小必须足以容纳其成员.它可以包含指向其他Node对象的指针,在这种情况下,可能就是您应该使用的指针:

A Node cannot contain another Node, because it must have a fixed size that is large enough to contain its members. It can contain pointers to other Node objects, and that's probably what you should use in this case:

private:
    Node *children[2];

这意味着您必须将对children数组的赋值重新编写(无论如何,它仍然是不正确的),以便它分配类型为Node *的值:

This means that you must re-write your assignment to the children array (which as it stands is incorrect anyway), so that it assigns values of type Node *:

// Incorrect:
// children[2] = {Node(false, this->depth + 1, maxDepth), Node(true, this->depth + 1, maxDepth)};
children[0] = new Node(false, this->depth + 1, maxDepth);
children[1] = new Node(true, this->depth + 1, maxDepth);

这篇关于错误:“孩子"字段的类型“节点[2]"不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆