Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward declaring a typedef of an unnamed struct [duplicate]

Possible Duplicate:
Forward declarations of unnamed struct

If I have

typedef struct tagPAGERANGE
{
    int iFirstPage;
    int iLastPage;
} PAGERANGE;

I can forward declare it that way

struct tagPAGERANGE;
typedef struct tagPAGERANGE PAGERANGE;

But what I have is

typedef struct
{
    int iFirstPage;
    int iLastPage;
} PAGERANGE;

I'm not sure how I can do it. I only want to hold a pointer to this struct. Right now I'm stuck with either including a rather substantial header, or duplicating the definition of the struct.

like image 915
sashoalm Avatar asked Apr 20 '12 15:04

sashoalm


2 Answers

It's impossible. You can only declare named structs.

Think about what identifies a struct that doesn't have a name, and how do you tell the compiler that it's that struct you want. If it doesn't have a name, it's identified by its members, so you need to provide members — i.e. define it. Therefore, you can't just declare it — you don't have a luxury of an identifier other than the definition itself.

like image 83
Cat Plus Plus Avatar answered Nov 09 '22 05:11

Cat Plus Plus


Since this is used in a C++ code, just get rid of the typedefs altogether, they are unnecessary and bad style in C++.

The real solution is to just use named structs:

struct foo; // forward declaration

struct foo {
    // … implementation
};

The typedefs are not useful.

like image 22
Konrad Rudolph Avatar answered Nov 09 '22 06:11

Konrad Rudolph