Jump to content
  • Advertisement
Sign in to follow this  
mezonx

forward declarations

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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<int>; typedef struct { ... } DataEx; <-- somewhere and in header somewere else: struct DataEx; I'm getting very strange errors when trying to do this thanks

Share this post


Link to post
Share on other sites
Advertisement
what ide are you using? (vc++ until 6 has problems with templates...)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
thanks!, it's now clear for me, but just one more small question :)
If I have typedef like this:
typedef Vector<int> 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

Share this post


Link to post
Share on other sites
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 <vector>, because it's a standard header, and guarenteed not to cause harm.

Share this post


Link to post
Share on other sites
Unfortunately you shouldn't forward declare things from within the standard library. You'll have to #include <vector>. 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!