Error C2143: syntax error : missing ';' before '*'

The question was asked: 6 years 9 months ago   views: 37
0

Why the compiler complains when you return a pointer to the object of the inner class in a method external? Suppose we have here is a template class:

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 method node min(node) and compile, the compiler complains tipo error C2143: syntax error : missing ';' before '*'. a rough 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 should find the minimum node in the key tree from the incoming pointer and return pointer to found or NULL if no such. What's the matter?

The VC++compiler.

The method should find the minimum node in the key tree from the incoming pointer and return pointer to found or NULL if no such. - 24-11-2012 в 08:18:24
And? put, nothing has changed. - 24-11-2012 в 09:26:23
This entire piece? And fully the message of the compiler, without "tipo", with the line number. I have this piece compiles fine. g++ v4.6.3 - 24-11-2012 в 09:46:32
added the description in the question - 24-11-2012 в 10:14:49
Added a second piece. On its first line the compiler responsibly says: e.c++:19:22: error: "node" is not a type name, the Number of rows can be shifted, I'm indent'om walked. - 24-11-2012 в 10:26:42

Answers   3

0

If you want to define this function outside of a class definition should be as follows:

template <class T> typename BST<T>::node * BST<T>::min (node* elem) { ........ }
  1. The type node is declared inside a template BST, so to work with it outside the class body it is necessary to write that it applies to him, and not just to him but to his instancecount specific instance
  2. Since BST<T> specifies no specific template arguments when referring to it's internal types have to specify that it is, and some static members. This is the key word typename
  3. All of this does not need to specify the second time (in function argument list), since you specify that the function belongs to a certain namespace, and the rule of some guy (don't remember his name) is the space is used.

Only problem is: how to use this feature for external users, if the return type is private. Or only for internal needs (and then its better to make private) or for user-friendly functions and classes. Think over this point.

Answered: 25-11-2012 в 01:14:44
@mikillskegg, Thank You very much! If not for your answer I would never know about such subtleties!=) Well, maybe they did, but not soon=) - 25-11-2012 в 10:04:14
0

And as the class can give out a pointer to the instance of a private inner class? This time.

Second, min something which is declared as a macro: #define min(a, b) ((a) < (b) ? (a) : (b)) or improved variants, it can "hit" compilation.

I tried your code on Visual Studio.

In General, things are even simpler: you forgot a semicolon at the end of the class Declaration.

Answered: 24-11-2012 в 15:05:34
2-take into account, but 1st inner class still remain in private, this is the idea, but then how to do, that he could give? - 24-11-2012 в 22:10:37
@Djonny: just a counter question-and how they will benefit? this all depends. - 24-11-2012 в 22:13:24
so simple, any actions with an inner class will be implemented through public methods. - 24-11-2012 в 22:16:12
@Djonny: look at the update in the answer :) - 24-11-2012 в 22:16:37
@Djonny: then you will have to convert this class to public, otherwise nobody will be able to dostupatsya. - 24-11-2012 в 22:17:45
0

If I'm wrong, let the experts on this TSE the double-cross will correct me (I'm not one of 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; }; };
Answered: 24-11-2012 в 10:48:32
The solution to proletarian: cheap and no frills. - 25-11-2012 в 01:24:52