Why does the compiler swear when I return a pointer to an object of an inner class in an outer method? Suppose we have a template class like this:

template <class T> class BST { int sizeBST; int fun_count; private: class node { public: int key; T data; int bal; node *left, *right; node(int, T); ~node(); } *head; public:BST(); node *min(node *); } 

So, after describing the node min (node ) method and compiling, the compiler swears, as in error C2143: syntax error : missing ';' before '*'. error C2143: syntax error : missing ';' before '*'. approximate description of the method:

 template <class T> node * BST <T>::min(node * elem) { if (elem != NULL) { while (1) { if (elem->left != NULL) elem = elem->left; else return elem; } } return NULL; }; 

The method must find the minimum node according to the key in the tree from the incoming pointer and return a pointer to the found one or NULL if there is none. What's the matter?

VC ++ compiler.

  • The method must find the minimum node by the key in the tree from the incoming pointer and return a pointer to the found one or NULL if there is none. - Djonny
  • AND? set, nothing has changed. - Djonny
  • Is this the whole piece? And the complete compiler message, without the "type", with the line number. I have this piece compiled fine. g ++ v4.6.3 - alexlz
  • added description in question - Djonny
  • Added a second piece. To its first line, the compiler responsibly declares: e.c ++: 19:22: error: “node” is not a name of the type. The number of lines can be shifted, I went through it with an indent. - alexlz

3 answers 3

If you want to define this function outside the class definition, you need to:

 template <class T> typename BST<T>::node * BST<T>::min (node* elem) { ........ } 
  1. The node type is declared inside the BST template, so to work with it outside the class body, you need to write that it applies to it, and not just to it, but to its specific instantiated
  2. Since BST <T> is indicated here without specific template arguments, when referring to its internal types, you need to specify that these are the types, and not some static members. This is the keyword typename
  3. All this does not need to be specified a second time (in the function argument list), since it is indicated that the function belongs to such a namespace and, according to the rule of some peasant (I do not remember his name), this space begins to be used.

Only problem remains: how to use this function for external users, if the returned type is private. Only for internal needs (and then it is better to make it private), or for friendly functions and classes. Think over this moment.

  • @mikillskegg, Thank you so much! If it were not for your answer, I would never have learned about such subtleties! =) Well, maybe I learned, but not soon =) - Djonny

How can a class issue a pointer to a copy of a private inner class? This time.

Secondly, min declared here and there as a macro: #define min(a, b) ((a) < (b) ? (a) : (b)) or improved versions, this can "break" the compilation.


I tried your code in Visual Studio.

In general, it is still easier: you forgot the semicolon at the end of the class declaration.

  • I will take into account the 2nd, and the 1st inner class will still remain in private, this is the idea, but how then can it be done, what could it give? - Djonny
  • @Djonny: immediately a counter question - how will they use it? it all depends on it. - VladD
  • so everything is simple, any actions with the inner class will be implemented through open methods. - Djonny
  • @Djonny: look at the update in response :) - VladD
  • @Djonny: then you have to convert this class to public, otherwise no one can access it. - VladD

If I am wrong, let the experts of this tce-two-cross correct me (I do not belong to them)

 template < class T > class BST { int sizeBST; int fun_count; private: class node { public: int key; T data; int bal; node *left, *right; node(int, T); ~node(); } *head; public:BST(); node *min(node * elem) { if (elem != NULL) { while (1) { if (elem->left != NULL) elem = elem->left; else return elem; } } return NULL; }; }; 
  • Proletarian solution: cheap and without frills. - skegg