473,654 Members | 3,035 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:'SplayNod e' 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(Spla yNode *&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 1914
a2z
On Sep 2, 2:00*pm, remlostime <remlost...@gma il.comwrote:
I have written a SplayTree class, what makes me wonder is when I
compiled it, it has something errors:
error:'SplayNod e' 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(Spla yNode *&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...@gma il.comwrote:
I have written a SplayTree class, what makes me wonder is when I
compiled it, it has something errors:
error:'SplayNod e' 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(Spla yNode *&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 objektorientier ter Datenverarbeitu ng
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.co mwrote:
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?Than ks
Sep 5 '08 #7
remlostime wrote:
On Sep 3, 3:17 am, Ian Collins <ian-n...@hotmail.co mwrote:
>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
1653
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 "C:\winroot\src\c++\stupid_tricks\functor_trick.exe" -I"C:\DEV-CPP\include\c++" -I"C:\DEV-CPP\include\c++\mingw32" -I"C:\DEV-CPP\include\c++\backward" -I"C:\DEV-CPP\include" -L"C:\DEV-CPP\lib" C:/winroot/src/c++/stupid_tricks/functor_trick.cpp: In member function
19
3545
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 to find the _current_ reporting page... #include <vector> #include <iostream>
6
12530
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
5555
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: using System; using System.Runtime.InteropServices; namespace System.Net.Bluetooth { public class BLUETOOTH_FIND_RADIO_PARAMS
1
3284
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, but throws a load of linker errors
9
13262
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 <hash_map>//from Standard template library //and some other headers
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 "iof_config.h" file. //**************************************************** //FILE : iof_library.h //****************************************************
2
2749
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 <string>
5
3418
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 __FOO_ #include "boost/filesystem.hpp"
2
14894
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
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.