forward declaration of typedef

Hello all.
1
2
3
4
5
6
7
8
9
10
11
struct mystruct
{
        int   i;
	double f;
} ;

typedef mystruct myotherstruct;

//the other .cpp file
struct mystruct;  //OK,this is a correct forward declaration.
struct myotherstruct; // error C2371(in vc2k8): 'myotherstruct' : redefinition; different basic types 


Why can't I forward declare myotherstruct?

what error do you get?

EDIT: whoops, didnt see

Try this:
1
2
3
4
5
typedef struct mystruct
{
     int i;
     double f;
} myotherstruct;
Last edited on
try this:
1
2
3
4
5
6
7
typedef struct mystruct
{
	int i;
	double f;
} myotherstruct;

struct myotherstruct;
closed account (zb0S216C)
Bare in mind that myotherstruct is an alias for mystruct. In your source file, you're forward declaring both mystruct and myotherstruct, which is the same class. So it's equivalent to this:

1
2
struct mystruct;
struct mystruct;

Wazzak
Last edited on
you can't define myotherstruct again, because you previously typedefed it as mystruct! try tp #define it instread of typedefing it, and themm #undef it before the forward declaration!
The compiler correctly issues an error because you are trying to redefine the name myotherstruct.

At first you defined this name as


typedef mystruct myotherstruct;

That is now myotherstruct is a synonim for struct mystruct. Then you are trying to define a new type with the same name as myotherstruct

struct myotherstruct;

So the compiler does not know how to interpretate the name myotherstruct either as struct mystruct or as a new type struct myotherstruct.

The C++ Standard does not allow to use a typedef name with class names because otherwise there will be ambiguity. So if you need a forward declaration you should write

struct mystruct
{
int i;
double f;
} ;

typedef mystruct myotherstruct;

//the other .cpp file
struct mystruct;

Last edited on
Topic archived. No new replies allowed.