Archived

This topic is now archived and is closed to further replies.

cryo75

Structure pointing to self??

Recommended Posts

I have the following structure:
	typedef struct tagQuad {
		D3DXVECTOR3 vCoords[4];
		QUADNODE*   qChildren[4];
	} QUADNODE;
I get an error on QUADNODE*: missing storage-class bla bla... Basically I want that the struct points to itself. This is for a quadtree. How can I do it?? Thanks, Ivan

Share this post


Link to post
Share on other sites
try forward declaring it...


struct QUADNODE;
typedef struct tagQuad
{
D3DXVECTOR3 vCoords[4];
QUADNODE* qChildren[4];
} QUADNODE;


Edit: This won't work, but kept it to show a bad example! Whee!


[edited by - stonicus on October 7, 2003 10:55:21 AM]

Share this post


Link to post
Share on other sites
That''s standardish for plain C, in C++ you don''t need to do any of that silly typedef stuff.

Here are the ways of doing it


//Old C method

typedef struct tagQUADNODE {
D3DXVECTOR3 vCoords[4];
tagQUADNODE* qChildren[4];
} QUADNODE;

//C++ method

struct QUADNODE {
D3DXVECTOR3 vCoords[4];
QUADNODE* qChildren[4];
};

Share this post


Link to post
Share on other sites
quote:
Original post by cryo75
Hey AikonIV, I tried the c++ method and it does not compile. the c method works though. Strange.


The first one works, because the struct doesn''t rely on the typedef statement during the structure. You can''t reference the name of the typedef until after the closing brace of the struct declaration, because it simply doesn''t exist at that point.


typedef struct tagQUADNODE {
D3DXVECTOR3 vCoords[4];
tagQUADNODE* qChildren[4]; // QUADNODE doesn''t exist here.

} QUADNODE; // It exists here, though.



It''s like trying to reference something that isn''t complete yet. The typedef needs all the information from the struct''s declaration, in order to create the typedef itself. That means, that until it''s processed the declaration, the typedef isn''t created.

-hellz

Share this post


Link to post
Share on other sites
Aikon is actually quite right in his code. This:

struct QUADNODE {
D3DXVECTOR3 vCoords[4];
QUADNODE* qChildren[4];
};

works nicely, a nice compact piece of forward declaration. Make sure you''re not using ''tagQUADNODE'' where you should be using ''QUADNODE''.

Aikon, if you read this, thanks for the heads-up. Never really realized I was using C-style decs before.

ld

Share this post


Link to post
Share on other sites
ok now I''ve got this:


struct QUADNODE {
D3DXVECTOR3 vCoords[4];
QUADNODE* qChildren[4];
};


Which compiles fine. But when I have a function like this:


QUADNODE* InitQuad(float x, float y, float w, UINT level)
{
QUADNODE* tmp = new QUADNODE;
return tmp;
}


I get the following errors:

c:\program files\c++\winapp\ctile.cpp(121) : error C2143: syntax error : missing '';'' before ''*''
c:\program files\c++\winapp\ctile.cpp(121) : error C2501: ''QUADNODE'' : missing storage-class or type specifiers
c:\program files\c++\winapp\ctile.cpp(122) : error C2501: ''InitQuad'' : missing storage-class or type specifiers
c:\program files\c++\winapp\ctile.cpp(122) : error C2556: ''int *__thiscall CTile::InitQuad(float,float,float,unsigned int)'' : overloaded function differs only by return type from ''struct CTile::QUADNODE *__thiscall CTile::InitQuad(float,float,float,
unsigned int)''

Ideas anyone??

Ivan

Share this post


Link to post
Share on other sites
quote:
Original post by liquiddark

Aikon, if you read this, thanks for the heads-up. Never really realized I was using C-style decs before.

ld


A little OT, but the reason the other way was used in C, is because if you didn''t typedef a struct name, you would have use the struct keyword before the struct name every time you used it. So people used the typedef method so they wouldn''t have to use ''struct'' all the time. In C++ however, that is no longer true. Example:


struct AStruct
{
int SomeVar;
};

//Old C method without using typedefs

void SomeFunc()
{
//AStruct A; //This would give an error

struct AStruct A; //This is how you would need to do it

}

//C++ method

void SomeFunc()
{
AStruct A; //This works now

struct AStruct A; //I ''think'' this works as well, not sure tho

}

Share this post


Link to post
Share on other sites
I personally was aware of that, having struggled with that syntax years ago. It is a salient point for the beginners, all the same.

Moreover, it''s a really nice example of a grammar expansion:

Expression :- typedef <i>type</i> (namestring);

type :- char | char*
| int | int*
| float | float*
| (...)
| struct (namestring) { <typelist> }

typelist :- <type>;
| <type>; <typelist>;

where '':-'' means "expands to" and ''|'' means "or". You see these grammatical expressions in, for example, the SQL 92 definition, but this is a simpler example

ld

Share this post


Link to post
Share on other sites