James Kanze
9/2/2008 9:52:00 AM
On Sep 2, 11:00 am, remlostime <remlost...@gmail.com> wrote:
> I have written a SplayTree class, what makes me wonder is when I
> compiled it, it has something errors:
> error:'SplayNode' has not been declared
> you can see the code below that the SplayNode has been declared in
> private, why? And how can i fix it?
> And I have tried another way that declared the SplayNode outside the
> SplayTree and it works well. It really makes me confused.
> /*
> struct SplayNode
> {
> SplayNode *left, *right;
> int key;};
>
> SplayNode *root, *nullNode;
> */
> class SplayTree
> {
> public:
> SplayTree()
> {
> nullNode = new SplayNode;
> nullNode->left = nullNode->right = nullNode;
> root = nullNode;
> }
> void rotateLeft(SplayNode *&node)
> {
> SplayNode *p = node->right;
> node->right = p->left;
> p->left = node;
> node = p;
> }
Member function definitions are compiled "as if" they
immediately followed the class. The declaration is compiled
when it is seen, however, which means that the declaration of
this function is only legal if the compiler has seen the symbol
SplayNode. (Note that it can be a forward declaration: you
simply add:
class SplayNode ;
at the top of the class definition.
> private:
> struct SplayNode
> {
> SplayNode *left, *right;
> int key;
> };
> SplayNode *root, *nullNode;
> };
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34