Connecting Tech Pros Worldwide Help | Site Map

The error about the struct in the class

remlostime
Guest
 
Posts: n/a
#1: Sep 2 '08
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;
}
private:
struct SplayNode
{
SplayNode *left, *right;
int key;
};
SplayNode *root, *nullNode;
};
a2z
Guest
 
Posts: n/a
#2: Sep 2 '08

re: The error about the struct in the class


On Sep 2, 2:00*pm, remlostime <remlost...@gmail.comwrote:
Quote:
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;
* * * * * * * * }
* * * * private:
* * * * * * * * struct SplayNode
* * * * * * * * {
* * * * * * * * * * * * SplayNode *left, *right;
* * * * * * * * * * * * int key;
* * * * * * * * };
* * * * * * * * SplayNode *root, *nullNode;
>
>
>
};- Hide quoted text -
>
- Show quoted text -
During comilation 'class SplayTree' has to see declaration for
'SplayNode'. To fix it put forward declaration of "struct SplayNode;"
before 'class SplayTree'.
James Kanze
Guest
 
Posts: n/a
#3: Sep 2 '08

re: The error about the struct in the class


On Sep 2, 11:00 am, remlostime <remlost...@gmail.comwrote:
Quote:
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.
Quote:
/*
struct SplayNode
{
SplayNode *left, *right;
int key;};
>
SplayNode *root, *nullNode;
*/
Quote:
class SplayTree
{
public:
SplayTree()
{
nullNode = new SplayNode;
nullNode->left = nullNode->right = nullNode;
root = nullNode;
}
Quote:
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.
Quote:
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
remlostime
Guest
 
Posts: n/a
#4: Sep 2 '08

re: The error about the struct in the class


Could someone give me some tips about how to declare the struct?
Ian Collins
Guest
 
Posts: n/a
#5: Sep 2 '08

re: The error about the struct in the class


remlostime wrote:

[which struct - please keep context]
Quote:
Could someone give me some tips about how to declare the struct?
As normal, you just have to move the member definitions of the "client'
class into a source file which include the header with the required
struct declaration.

--
Ian Collins.
remlostime
Guest
 
Posts: n/a
#6: Sep 5 '08

re: The error about the struct in the class


On Sep 3, 3:17*am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
remlostime wrote:
>
[which struct - please keep context]
>
Quote:
Could someone give me some tips about how to declare the struct?
>
As normal, you just have to move the member definitions of the "client'
class into a source file which include the header with the required
struct declaration.
>
--
Ian Collins.
But whay when i put the struct in the private, it's wrong?
remlostime
Guest
 
Posts: n/a
#7: Sep 5 '08

re: The error about the struct in the class


who can give me a correct class definition?Thanks
Ian Collins
Guest
 
Posts: n/a
#8: Sep 5 '08

re: The error about the struct in the class


remlostime wrote:
Quote:
On Sep 3, 3:17 am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
>remlostime wrote:
>>
>[which struct - please keep context]
>>
Quote:
>>Could someone give me some tips about how to declare the struct?
>As normal, you just have to move the member definitions of the "client'
>class into a source file which include the header with the required
>struct declaration.
>>
>
But whay when i put the struct in the private, it's wrong?
The compiler doesn't know what it is until it has finished parsing the
class declaration. Either do as I said and put the containing class
member function definitions in a source module, forward declare the
nested struct or put your private parts before your public ones.

--
Ian Collins.
Closed Thread