473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with declarations (simple question)

Okay, I have this sample code:

class a;
class b;

class b
{
public:

private:
class a foo;
}

class a
{};

I get this compilation error saying that there's an error with the
forward declaration and that i'm doing an invalid use of an undefined
type. I'm not sure how to get the compiler to recognize that class a
will be defined later and it's okay if I use it as a type now.

So how do I get the compiler to let me declare variables with a class
that hasn't been defined yet?

Aug 7 '05 #1
8 1465
twoeyedhuman111 1 wrote:
Okay, I have this sample code:

class a;
class b;

class b
{
public:

private:
class a foo;
}

class a
{};

I get this compilation error saying that there's an error with the
forward declaration and that i'm doing an invalid use of an undefined
type. I'm not sure how to get the compiler to recognize that class a
will be defined later and it's okay if I use it as a type now.

So how do I get the compiler to let me declare variables with a class
that hasn't been defined yet?


You can't. You can declare variables of type *pointer to* a class that
hasn't been fully defined, and you can declare variables of type
*reference to* a class that hasn't been fully defined, but you can't
declare a plain variable of that type.

Also, in your declaration of b, you don't need the keyword "class" in
front of "a foo;"
Aug 8 '05 #2
I guess I can't make a binary tree then.. Because I have two classes in
my binary tree:
xbasednode
ybasednode

I don't really need to explain exactly what they do, but they both use
each other in their source code, and in the same way in the above
example..

Perhaps with extern I could defeat my compiler...
Also, in your declaration of b, you don't need the keyword "class" in
front of "a foo;"


Yeah.. I didn't see that lol. Nice job seeing that.

Aug 8 '05 #3
twoeyedhuman111 1 wrote:
I guess I can't make a binary tree then.. Because I have two classes in
my binary tree:
xbasednode
ybasednode

I don't really need to explain exactly what they do, but they both use
each other in their source code, and in the same way in the above
example..

Perhaps with extern I could defeat my compiler...
Also, in your declaration of b, you don't need the keyword "class" in
front of "a foo;"


Yeah.. I didn't see that lol. Nice job seeing that.

Let's think about how much sense that makes.

Suppose you *could* do what you (think) you want to, i.e. have two
classes, again call them A and B, where every B has an instance of A in
it and every A has an instance of B in it. Question: How big is an
instance of A?

*Pointers*, however, do not exhibit this problem; the compiler *knows*
how big the pointer to the instance of a class is.

Why don't you show some (closer) to real code to illustrate what you're
trying to do?

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Aug 8 '05 #4
twoeyedhuman111 1 wrote:
I guess I can't make a binary tree then.. Because I have two classes in
my binary tree:
xbasednode
ybasednode

I don't really need to explain exactly what they do, but they both use
each other in their source code, and in the same way in the above
example..

Perhaps with extern I could defeat my compiler...


Probably not. You seem to think that you are dealing with some arbitrary
restriction that somehow made it into the language definition. However,
there is a valid reason *why* two classes cannot mutually contain fields of
the other type: at some point the compiler has to determine the size of
objects of type xbasenode and of objects of type ybasenode. If xbasenode
objects have, among other data members, a field of type ybasenode, you get

sizeof(xbasenod e) > sizeof(ybasenod e)

and conversely from the other inclusion you get

sizeof(ybasenod e) > sizeof(xbasenod e)

This is a contradiction that cannot be resolved. Hence the restriction to
pointers: their size is not a problem.
Best

Kai-Uwe Bux
Aug 8 '05 #5
Okay so I guess doing things the way I'm doing them is bad design and
makes the compiler sick. Perhaps I could combine both classes into
one... The only differences between xbasednode and ybasednode are the
way they handle their input.

The xbasednode and ybasednode store sprites and sort them by x and y
respectively.

ybasednode's code is so unreadable right now, that I can't even
understand it...

xbasednode's code: (beware, it's very long for readability)
class xbasednode
{
public:
xbasednode() {
haschildren = false;
};
~xbasednode();
// checks whether this is a right or left
// based child and tells parent it's dying;
void rChildIsBeingDe stroyed() {
rightnode = NULL;
};
void lChildIsBeingDe stroyed() {
leftnode = NULL;
};
void amNowRightNode( ) {
/* tells this node that it's a right node */
childside = RIGHTBASEDNODE;
};
void amNowLeftNode() {
/* tells this node that it's a left node */
childside = LEFTBASEDNODE;
};
ybasednode * GetRight() const {
if (rightnode != NULL) return rightnode;
/* else */
return NULL;
};
void SetRight(ybased node * daNode) {
rightnode = daNode;
rightnode->amNowRightNode ();
haschildren = true;
};
void MakeRight() {
rightnode = new ybasednode;
rightnode->amNowRightNode ();
/* tell its a right based node*/
haschildren = true;
};

ybasednode * GetLeft() {
if (leftnode != NULL) return leftnode;
/* else */
return NULL;
};
void SetLeft(ybasedn ode * daNode) {
leftnode = daNode;
leftnode->amNowLeftNode( );
haschildren = true;
};
void MakeLeft() {
leftnode = new ybasednode;
leftnode->amNowLeftNode( );
haschildren = true;
};
void SetParent(ybase dnode * parentToBe) {
parent = ParentToBe;
};
ybasednode * GetParent() {
return parent;
};
// void addToList(sprit e * x) { itsdata.push_ba ck(x); /*add a
new sprite*/};

// if we have children for these next 3
//, pass these parameters on to the children
// remove a sprite from list by id
void removeFromList( int id);
// remove a sprite from list by x,y
void removeFromList( int x, int y);
// remove sprites within a rectangle
void removeFromList( t3h::rect x);
void update();

void draw();

private:
int childside; // tells whether this is a right or left child
bool haschildren;
std::vector<spr ite*> itsdata;
ybasednode * parent; // the reason these are y
// based is so that x and y nodes
// repeat each other
ybasednode * rightnode;
ybasednode * leftnode;
t3h::rect itsarea;
};

//------------------------------------------------
void xbasednode::Dra w()
{
for (int a = 0; a != itsdata.size(); a++)
itsdata[a]->Draw();
}

// ------------------------------------------------
void xbasednode::Upd ate()
{
int temp_x1, temp_y1, temp_x2, temp_y2,
temp_x3, temp_y3, temp_x4, temp_y4; // for collision detection int

sizeoflist = itsdata.size(); // check collision with all
//other sprites except itself

for (int a = 0; a != sizeoflist; a++)
{
itsdata[a]->Update();

// test a collision with every other sprite
for (int j = a+1; j < sizeoflist; j++)
{
// see my paper about how this works

temp_x1 = itslist[a]->GetX();

temp_y1 = itslist[a]->GetY();

temp_x2 = temp_x1 + itslist[a]->GetWidth();

temp_y2 = temp_y1 + itslist[a]->GetHeight();
// just to make sure we don't go over our list
if (a+1 != itslist.size()+ 1)
{
temp_x3 = itslist[a+1]->GetX();

temp_y3 = itslist[a+1]->GetY();

temp_x4 = temp_x3 + itslist[a+1]->GetWidth();

temp_y4 = temp_y3 + itslist[a+1]->GetHeight();
}

if ((temp_x4 > temp_x1 > temp_x3
&& temp_y3 < temp_y1 < temp_y4) ||
(temp_x4 > temp_x2 > temp_x3 && temp_y3 < temp_y1 < temp_y4) ||
(temp_x4 > temp_x1 > temp_x3 && temp_y3 < temp_y2 < temp_y4) ||
(temp_x4 > temp_x2 > temp_x3 && temp_y3 < temp_y2 < temp_y4))

// if one of sprites points is inside one of the other sprites
// points, then a collision has been detected

itslist[a]->CheckCollision (itslist[a+1]);

// so lets tell the sprites that then, shall we?
}
}
// if we've exceeded our size limit, then
// lets pass our sprites onto the children
if (sizeoflist > MAXIMUMLISTSIZE )

{

// of course, for safety reasons, don't
//create more children when we have them, lol
if (haschildren == false)
{
rightnode = new ybasednode;

rightnode->amNowRightNode (); /* tell its a right based node*/

leftnode = new ybasednode;

leftnode->amNowLeftNode( ); /* tell its a left based node*/

// now that we've had sex and had children,
//lets sort out our sprites and give them to our children
//just like a will

for (int a = 0; a < sizeoflist; a++)
{

//if (itsdata[a].GetY() <

}

}

}

} // end of update function

Aug 8 '05 #6
Ian
twoeyedhuman111 1 wrote:
I guess I can't make a binary tree then.. Because I have two classes in
my binary tree:
xbasednode
ybasednode

I don't really need to explain exactly what they do, but they both use
each other in their source code, and in the same way in the above
example..

If A contained a B and B contained an A, which contains a B.......

Use pointers.

Ian
Aug 8 '05 #7
twoeyedhuman111 1 wrote:
Okay so I guess doing things the way I'm doing them is bad design and
makes the compiler sick. Perhaps I could combine both classes into
one... The only differences between xbasednode and ybasednode are the
way they handle their input.

The xbasednode and ybasednode store sprites and sort them by x and y
respectively.

ybasednode's code is so unreadable right now, that I can't even
understand it...

xbasednode's code: (beware, it's very long for readability)
class xbasednode
{
public:
[snip] private:
int childside; // tells whether this is a right or left child
bool haschildren;
std::vector<spr ite*> itsdata;
ybasednode * parent; // the reason these are y
// based is so that x and y nodes
// repeat each other
ybasednode * rightnode;
ybasednode * leftnode;
t3h::rect itsarea;
};

Although you posted *way* too much code -- and you never really stated
what the problem was, I think I have an inkling as to what's going on.

Naturally, it's hard to evaluate your design (and at these prices, it
ain't gonna happen), but I immediately notice one thing: Your types are
mutually recursive in regards to *pointers* which is just fine.

The way to solve your problem is by judicious separation of declaration
and implementation. Your *implementation * code for xbasednode needs to
be able to see the *declaration* of ybasednode, and vice versa. This is
not a problem.

I would, however, recommend that you post a *minimal* extract (the
smallest possible one that gives you trouble) and you'll either solve it
by yourself in the process or someone here should be able to help you
quickly and easily.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Aug 8 '05 #8
Actually, I did solve it, with your help. I combined the two classes
into one. I'm not sure if I can really post what I did, since my brain
is racked, but I did solve the problem.

I learned that two classes cannot have each other as members and so I
combined the two classes and solved the problem.

So thank you very much for helping me! I got my problem fixed! yayay!!

Aug 8 '05 #9

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

Similar topics

9
1351
by: Michael Tobis | last post by:
Summary of my understanding of a recent interesting thread: General usage has "declaration" meaning "statement which does not generate executable bytecode but merely affects the compiler". My assertion that decorator syntax is "declarative" is therefore formally false. The common assertion that "Python is 100% executable" is an exageration, but not because of the introduction of decorator syntax. The "global" statement, however is not...
23
3083
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
2
4444
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist then skip this file and move onto the next file (line). If file is there then move to a sirectory. Here is the code I have (Feel free to make corrections as needed. If possible make changes in red)
17
2540
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and operator overloading and virtual functions. I mean it should not hamper the C++ meaning. In frank and simple terms i need to implement a small C++ compiler in C++, and i want the intermediate representation to be C. Please help me in this....
23
2417
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file, there are lots of errors and warnings in compiling stage. I am really confused about this problem because I'm not even familiar with C++ and my code only includes simple C functions. Can anybody please tell me what's my mistake? Is there any other...
21
3225
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
12
1872
by: nephish | last post by:
Hello there, i am getting to need to make my web stuff more OO. i have a project at work that we are porting to the internet, and i started learning php to do so. the project is now mamoth is size, and code is reused and pasted all over it. I plan to do a good re-write using better, and cleaner code. So i want to incorporate some classes. So i have some questions.
6
7866
by: Rolf Welskes | last post by:
Hello, if I have for example: <table style="width: 100%; height: 100%;" border="1"> <tr> <td style="width: 100px">k </td> <td style="width: 100px">k </td> </tr>
15
2175
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this is because the compiler needs to see the implemenation of A when allocating memory for a_obj. ...
0
9480
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
10090
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
9949
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
8971
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
7499
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
5380
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.