Forward Declarations to Typedefs?

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

Recommended Posts

Has anybody else noticed that VC++ in VS.NET 2003 will crash and burn if you try to use a forward declaration to a typedef'd type? You can test this out yourself. Create a main, and 2 others files. In one file create a blank struct called class1. In the other header file create a forward declaration for a class3 and then create a struct that only has one member, a class3 type. In the main file, include the class1 file and then setup the typedef class1 class3, and then include the class2 file. Create an instance of class2 in your main function and compile. You'll get the lovely fatal error C1001: INTERNAL COMPILER ERROR. This is really annoying and I'm currently looking for a workaround. Right now all I can figure is to either drop the typedef all together ( which is not an option ) or to use a preprocessor directive instead of a typedef, which will ruin intellisense and create just generally ugly code. As it stands, I'm having to drop the forward declarations which is annoying since it seriously bloats the compile time. Has this problem been fixed in the express versions? Any ideas on better workarounds?

Share on other sites
Are you complaining because it's crashing on valid code? Or is your code invalid? If the latter, explain what you're trying to do better...

Share on other sites
The code is valid and simple. Use a forward declaration to a typedef'd class and then instantiate the class that was using the forward declaration. It will not compile.

Share on other sites
You typedef'ed something to the same name as a class?

Share on other sites
Why not forward declare the class and then declare your typedef before you use it? If you do it that way, you shouldn't need to use a forward declared typedef.

Share on other sites
Do you mean something like this ?:

Class1.h
class Class1{} ;

Struct1.h
class Class 3 ;struct Struct1{    Class3 AMember ;} ;

main.cpp
#include "Class1.h"typedef Class1 Class3 ;int main (){    Class3 AClass ;    return 0 ;}

It compiles fine on my VS 2003.

Share on other sites
Quote:
 Original post by Skeleton_V@T...

Almost, change that to:

-- Class1.h
class Class1
{
} ;

-- Struct1.h
class Class3;
struct Struct1
{
Class3 AMember ;
};

-- Main.cpp
#include "Class1.h"
typedef Class1 Class3 ;
#include "Struct1.h"

int main ()
{
struct1 str;
return 0 ;
}

Share on other sites
Quote:
 Original post by nimrandWhy not forward declare the class and then declare your typedef before you use it? If you do it that way, you shouldn't need to use a forward declared typedef.

I'm not sure I exactly understand what you mean? If I do understand you, then declaring the class before I use it would completely bypass the purpose of the forward declarations - to speed up compile time. And there is also the issue that there is one typedef and multiple classes uses the typedef'd class. Again, maybe I'm not understanding what you're suggesting?

Share on other sites
Well you can't declare a member to be an incomplete type. And I can't imagine why you would want to typedef a class to the name of another class, nor could I think of what it might do, if any compiler allows it.

Share on other sites
Quote:
 Original post by RDragon1Well you can't declare a member to be an incomplete type. And I can't imagine why you would want to typedef a class to the name of another class, nor could I think of what it might do, if any compiler allows it.

Class3 is not an 'incomplete' type or an independant class; it is an alias for class1.

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• Forum Statistics

• Total Topics
632952
• Total Posts
3009430
• Who's Online (See full list)

There are no registered users currently online

×