Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Forward declarations

Reply
Thread Tools

Forward declarations

 
 
Carlos Martinez Garcia
Guest
Posts: n/a
 
      02-16-2006
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.
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      02-16-2006
"Carlos Martinez Garcia" <(E-Mail Removed)> wrote in message
news:dt1jcr$(E-Mail Removed)
> 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;


--
John Carson


 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      02-16-2006
"Carlos Martinez Garcia" <(E-Mail Removed)> wrote in message
news:dt1jcr$(E-Mail Removed)...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
forward declarations and namespaces? Steven T. Hatton C++ 6 05-05-2004 01:26 PM
NEED HELP with forward declarations Alan Lee C++ 5 04-05-2004 01:03 PM
Forward declarations and namespaces whithers C++ 4 01-16-2004 07:12 AM
Some problems with forward declarations mjm C++ 3 08-13-2003 12:48 AM
namespaces and forward declarations matthew polder C++ 1 07-24-2003 04:06 PM



Advertisments