0

to start of let me just explain what i am trying to accomplish here, basicly im doing some basic pathfinding in a game, and for this i have constructed a very simple class called Node and inside of node i have some variables and functions, but in order for my pathfinding algorithm to work i will need to be able to go back to the parent node of the current node that i am looking at, so i need every Node to contain their parent Node, it mgiht be easier to understand in code so ill write down some sample code.

i have tried doing this but im just getting an error like "Incomplete type is not allowed" so im guessing im doing something very wrong here.

class Node {
      int f_score, g_score, h_score; //just a few variables used for pathfinding.
      Node parent_node; //parent node as explained above
      Node(void); //just a function.
}

so its pretty easy to see what im trying to accomplish here, im just trying to have an object of the same class kind of, sorry if im hard to understand, but i tried my best to explain, any help will be greatly appreciated.

Paze
  • 191
  • 1
  • 13
  • 4
    use pointer instead `Node* parent_node;`, or better `std::weak_ptr parent_node;` – Piotr Skotnicki Oct 01 '14 at 18:01
  • It is impossible to use any object with unknown size as member (here Node is incomplete and it's final size is unknown) –  Oct 01 '14 at 18:08
  • `Node(void);` not *just* a function, but a *constructor*. – crashmstr Oct 01 '14 at 18:18
  • 1
    What would the size of such a Node object be? Let's see, it would be the size of (three ints plus one Node member-object). What would the size of that Node member-object be? The size of (three ints plus one Node member object). [...] Conclusion: A Node containing a Node member-object would be infinitely large, so you're going to need to buy more RAM if you want to use it :) – Jeremy Friesner Oct 01 '14 at 18:22

3 Answers3

0

Since the definition of class Node is not complete by the time you included a member of type Node in the class itself, the compiler cannot yet determine how much space to allocate for the member (this is why it's said to be "incomplete"). Instead, use type Node* for the member, or std::auto_ptr<Node> if using C++0x, or std::weak_ptr<Node> if using C++11.

See also: Next struct item,incomplete type

Community
  • 1
  • 1
inspector-g
  • 4,146
  • 2
  • 24
  • 33
0

You must limit the member field to a pointer (Node*).

You can't use an inline object member because, at that point, the compiler has not yet "closed" the Node type.

  1. Requires knowledge of the storage size of the object
  2. Would create an infinitely recursive type definition

Your original sample would create a Node containing a Node containing a Node... You can get away with similar notation in Java or C# because inline objects are actually references (pointers), and not inlined like C++.

class Node {
   Node * parent;
public:
   Node() : parent(nullptr) {}
   Node(Node*parent)
      : parent(parent)  // this is ok
   {
   }
}; // type is now closed
codenheim
  • 20,467
  • 1
  • 59
  • 80
  • ahh okay, i see, thank you mate :) i will accept ur answer as the solution. – Paze Oct 01 '14 at 18:11
  • The answer is not correct: A constructor: `Node() : parent(new Node) {}` is perfectly fine –  Oct 01 '14 at 18:13
  • Your phrase `out of line` is confusing. It is possible to use a `Node` (not meaning a Node*) inside any constructor/function defined inline in the class body. –  Oct 01 '14 at 18:41
  • @Paze - Sorry mate, you better reread my answer now that I corrected (I hope). I think I learned more than you in this question. Sorry for misleading you with misinformation. I've never let ignorance of C++ stop me from using it. :) – codenheim Oct 01 '14 at 18:57
0

Make it public. Try this:

    class Node {
        //Make it public so it's accessible.
        public:
            int f_score, g_score, h_score;
            Node* parent_node;
            //Make the "Node(void);" function do something or take it out.
    };

Oh, yeah, and make the function do something or remove it. For me, it was causing build errors when i tried it. It worked when i removed the function.