Skip to main content
GameDev.net gamedev.net
🔒 Locked

forward declarations

Started by mezonx Aug 10, 2005 at 3:23 PM 7 replies 2.6k views
Original Post
mezonx
mezonx
hi, is it possible to write forward declarations of structs or template classes? I mean sth similar to: class Dummy; in header file for example: class Vector; typedef struct { ... } DataEx; <-- somewhere and in header somewere else: struct DataEx; I'm getting very strange errors when trying to do this thanks
ju
ju
what ide are you using? (vc++ until 6 has problems with templates...)
Hey Ho Let's Go!
SirLuthor
SirLuthor
Enjoy:

[EDIT] I did indeed forget to mention that, Erzengeldeslichtes. I was assuming he was declaring his structs like classes, as is done in C++, not in the ugly C way, which is, by the way, fairly obsolete. [smile]

struct Example;template <class T>class ExampleT;


[Edited by - SirLuthor on August 10, 2005 3:29:42 PM]
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Erzengeldeslichtes
Erzengeldeslichtes
Sir Luthor has pretty much told you what you want to know but there's a bit of a problem:
struct DataEx;
this refers to a structure DataEx.
typedef struct {} DataEx;
this refers to a structure that gets its name from the compiler and is usually named something like __unnamed1234, and then that unnamed structure is typedef'd to also be called DataEx. The structure is not DataEx, though.

If you use these two, you will get compile error C2371: redefinition; different basic types. (Using VisualC++ 2005 beta 2)

For example:
struct DataEx;DataEx* Temp;typedef struct{	int a;} DataEx;void main(){	Temp->a = 5;}

gives 3 errors stemming from the fact that DataEx is defined as an alias but declared as a structure.

Instead, you should do this:
struct DataEx;DataEx* Temp;struct DataEx{	int a;};void main(){	Temp->a = 5;}

which will then work fine in C++ (which, since you're using forward declarations, I assume you are using. The assumption is supported by your usage of templates).
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
mezonx
mezonx
thanks!, it's now clear for me, but just one more small question :)
If I have typedef like this:
typedef Vector VectorInt;
and then want to use it in header without including Vector template file, like this:
class {
...
VextorInt* pVI;
...
};

then how should forward declaration looks like?

Quote:
Original post by ju
what ide are you using? (vc++ until 6 has problems with templates...)

I'm using VC2003, the problem is that I've just started using templates recently and trying to learn about them
SirLuthor
SirLuthor
I could be wrong, but I believe you have to both forward declare the std::vector class itself, as well as typedef'ing it. Although I don't see much point in not just doing #include , because it's a standard header, and guarenteed not to cause harm.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Qw3r7yU10p!
Qw3r7yU10p!
Unfortunately you shouldn't forward declare things from within the standard library. You'll have to #include . I find it best to put all library headers such as the standard library, boost, etc, into a precompiled header. It makes things much faster to compile.

In related news, iostream is quite a heavy-weight header and the standard committee decided to include a forwarding header, iosfwd which is relatively lightweight.
mezonx
mezonx
I didn't have STL's vector container in mind, just coincidence :) In fact this vector template class is part of my little math lib.
This method of forward declaring and typedefing works but it's too ugly for me (and redundant) so I think that I will just move all typedefs like this to precompiled header like petewood said.
Once again thx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.