[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

The error about the struct in the class

remlostime

9/2/2008 9:00:00 AM

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;
};
7 Answers

a2z

9/2/2008 9:33:00 AM

0

On Sep 2, 2:00 pm, 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;
>                 }
>         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

9/2/2008 9:52:00 AM

0

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

remlostime

9/2/2008 3:28:00 PM

0

Could someone give me some tips about how to declare the struct?

Ian Collins

9/2/2008 7:18:00 PM

0

remlostime wrote:

[which struct - please keep context]

> 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

9/5/2008 12:30:00 AM

0

On Sep 3, 3:17 am, Ian Collins <ian-n...@hotmail.com> wrote:
> remlostime wrote:
>
> [which struct - please keep context]
>
> > 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

9/5/2008 10:50:00 AM

0

who can give me a correct class definition?Thanks

Ian Collins

9/5/2008 8:49:00 PM

0

remlostime wrote:
> On Sep 3, 3:17 am, Ian Collins <ian-n...@hotmail.com> wrote:
>> remlostime wrote:
>>
>> [which struct - please keep context]
>>
>>> 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.