Forward Declarations to Typedefs?

Started by
16 comments, last by RDragon1 18 years, 3 months ago
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?
Advertisement
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...
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.
You typedef'ed something to the same name as a class?
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.
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.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
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 ;
}

Quote:Original post by nimrand
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.


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?
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.
Quote:Original post by RDragon1
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.


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

This topic is closed to new replies.

Advertisement