Wednesday, 15 January 2014

c++ - Having problems trying to use a private class declared in header file in the cpp file -


I am implementing a binary tree and for doing so I have 2 files, a header and one implementation. CPP file is. In the header file I have declared a class 'node' in the private sector, and In the CPP file I have a function that gives 'node *', but when I try to compile my code, then I will get an "unknown type" 'node' function declaration.

Contextual Code:

 In  // Private: Class node {public: node * left; node * right; int data; node (int n) {it-> data = n; It-> left = null; it-> true = null;}}; // in.cpp node * Binary_Ordered_Tree_int :: insert (node ​​* n, int i) {if (n == NULL) {new Return the node (i);} If (n-> data is } i {n-> Enter true = (n-> true, i);} other {n-> left = (n-> left, i);} return n;}  
< P> Thank you in advance.

Because the type of return comes in the function name before (and its class ), The compiler does not know that he should look in the class, so you can either tell it:

Preferences> Binary_Ordered_tree_int :: node * Binary_Ordered_tree_int :: insert (node ​​* n, int i) / Code>

Or, if you have C ++ 11 available, you use the back type So that the type of return is in the context of the class:

  Auto Binary_Ordered_tree_int :: insert (node ​​* n, int i) -> Node *  

No comments:

Post a Comment