8

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.

Community
  • 1
  • 1
sashoalm
  • 75,001
  • 122
  • 434
  • 781

2 Answers2

13

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.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
2

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    The header file that uses those typedefs is not mine, and it's designed to work in both C and C++. – sashoalm Apr 20 '12 at 16:17
  • @satuon In that case see the duplicate question. As the cat said, this is simply not possible but the other question shows a (hideous) workaround. – Konrad Rudolph Apr 20 '12 at 16:18