473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The error about the struct in the class

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;
};
Sep 2 '08 #1
7 1902
a2z
On Sep 2, 2:00*pm, remlostime <remlost...@gmail.comwrote:
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'.
Sep 2 '08 #2
On Sep 2, 11:00 am, remlostime <remlost...@gmail.comwrote:
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:ja*********@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
Sep 2 '08 #3
Could someone give me some tips about how to declare the struct?
Sep 2 '08 #4
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.
Sep 2 '08 #5
On Sep 3, 3:17*am, Ian Collins <ian-n...@hotmail.comwrote:
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?
Sep 5 '08 #6
who can give me a correct class definition?Thanks
Sep 5 '08 #7
remlostime wrote:
On Sep 3, 3:17 am, Ian Collins <ian-n...@hotmail.comwrote:
>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.
Sep 5 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1638
by: Zach | last post by:
Using Dev-C++ 4.9.8.5 I get error: Compiler: Default compiler Executing g++.exe... g++.exe "C:\winroot\src\c++\stupid_tricks\functor_trick.cpp" -o...
19
3528
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
6
12509
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
3
5539
by: B Vidyadhar Joshi | last post by:
I was writing an Bluetooth Application which makes calls to Windows APIs. I feel I'm doing something wrong with the structure. Could somebody help me? The code that I'm using is pasted below: ...
1
3263
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
9
13241
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
0
507
by: Taran | last post by:
Hi all, I have this config.h file which has all the declarations for the vars being used in the application. There are no compilation errors but link errors for all the vars declared in this...
2
2732
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
5
3410
by: GaryE | last post by:
Hello: I am having trouble linking a couple of files using the boost::filesystem. I am using MSVC 6.0. Here is an abbreviated version of my problem: foo.h: #ifndef __FOO_ #define...
2
14883
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.