The error about the struct in the class

R

remlostime

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

a2z

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'.
 
J

James Kanze

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.
 
I

Ian Collins

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.
 
R

remlostime

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?
 
I

Ian Collins

remlostime said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top