Leonhart Squall
Leonhart Squall

Reputation: 850

declare typedef type

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

Hi all. Why can't I forward declare myotherstruct?

Upvotes: 6

Views: 1640

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

The myotherstruct identifier is not a struct tag, it is a type name in its own rights. You use it without the struct keyword. Once defined, the name cannot be reused for a struct tag. In your example, you are not forward-declaring myotherstruct type, you are forward-declaring a struct with the tag myotherstruct, which gives you an error because the name myotherstruct has already been taken for the typedef.

Upvotes: 1

Andrew
Andrew

Reputation: 24846

You can't forward declare typedefs without forward declaration of the struct that is typedefed. You should first forward declare the struct and then typedef

struct mystruct;
typedef mystruct myotherstruct;

Upvotes: 1

Related Questions