Forward declarations

  • Thread starter Carlos Martinez Garcia
  • Start date
C

Carlos Martinez Garcia

Hi all:

I usually make forward declarations in headers. Something like this:

class MyClass;

Now, I need a reference to a type defined like this (traditional C Style):

typedef struct {


} DatosCdrGprs;

In my header I have a declaration:

class DatosCdrGprs;

but compiler says me there is a conflict between types "struct
DatosCdrGprs" (in my header file, at the line where is forward
declaration), and "typedef struct DatosCdrGprs DatosCdrGprs" (in the
file where is that type).

I tried also with forward declarations like:
struct DatosCdrGprs;
typedef struct DatosCdrGprs;
typedef struct DatosCdrGprs DatosCdrGprs;

but I always get errors.

¿How can I make the forward declaration?

Thanks in advance.
 
J

John Carson

Carlos Martinez Garcia said:
Hi all:

I usually make forward declarations in headers. Something like this:

class MyClass;

Now, I need a reference to a type defined like this (traditional C
Style):
typedef struct {


} DatosCdrGprs;

In my header I have a declaration:

class DatosCdrGprs;

but compiler says me there is a conflict between types "struct
DatosCdrGprs" (in my header file, at the line where is forward
declaration), and "typedef struct DatosCdrGprs DatosCdrGprs" (in the
file where is that type).

I tried also with forward declarations like:
struct DatosCdrGprs;
typedef struct DatosCdrGprs;
typedef struct DatosCdrGprs DatosCdrGprs;

but I always get errors.

¿How can I make the forward declaration?

Thanks in advance.


struct DatosCdrGprs;

typedef struct DatosCdrGprs
{}DatosCdrGprs;
 
I

Ivan Vecerina

: Hi all:
:
: I usually make forward declarations in headers. Something like this:
:
: class MyClass;
:
: Now, I need a reference to a type defined like this (traditional C
Style):
:
: typedef struct {
:
:
: } DatosCdrGprs;
Ah, the good old C idiom.
This defines DatosCdrGprs as an 'alias' to the anonymous
struct being defined.

: In my header I have a declaration:
:
: class DatosCdrGprs;
:
: but compiler says me there is a conflict between types "struct
: DatosCdrGprs" (in my header file, at the line where is forward
: declaration), and "typedef struct DatosCdrGprs DatosCdrGprs" (in the
: file where is that type).
Yes, this unfortunately won't work.
In C, struct names are in their own "namespace" (not in the C++
meaning of the word), independent from typedefs.
So the following code is legal in C:
typedef int S;
struct S { int a; };
void f( S i, struct S j ); // two different param types
Even though the previous is not legal in C++, C++ does
partially inherit this behavior...

: I tried also with forward declarations like:
: struct DatosCdrGprs;
: typedef struct DatosCdrGprs;
: typedef struct DatosCdrGprs DatosCdrGprs;
:
: but I always get errors.
:
: ¿How can I make the forward declaration?

A typedef cannot be forward-declared.
So you need to give a name to the struct :

Definition:
typedef struct DatosCdrGprs_struct {


} DatosCdrGprs;

Equivalent forward-declaration:
typedef struct DatosCdrGprs_struct DatosCdrGprs;



Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top