Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c++ - error: field ‘children’ has incomplete type ‘Node [2]

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

And the code:

#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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The real problem is that "Node" is undefined at the time "Node children[2];" is encountered by the compiler. You can have a pointer to it, but can't have an instantiation of it until it is fully defined (upon reaching the closing "}" ).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...