473,569 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forward Reference for Typedef'd Type?

I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?

Thanks,
Mike

May 18 '06 #1
7 15260
Michael B Allen schrieb:
I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?

Thanks,
Mike


You have to use the full type identifier, i.e.

typedef struct foo foo_t;

in your case. This will create an alias for struct foo called foo_t.

--
Marc Thrun
http://www.tekwarrior.de/
May 18 '06 #2
On Thu, 18 May 2006 23:43:06 +0200, Marc Thrun wrote:
Michael B Allen schrieb:
I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?

Thanks,
Mike


You have to use the full type identifier, i.e.

typedef struct foo foo_t;

in your case. This will create an alias for struct foo called foo_t.


But unlike a forward reference this is a type definition. And because I
need it in multiple header files I will start to get redefinition errors.

I'm toast.

Mike

May 18 '06 #3


Michael B Allen wrote On 05/18/06 17:26,:
I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?


Your problem isn't the forwardness of the reference,
but that you haven't written the typedef properly. Try

typedef struct foo foo_t;

--
Er*********@sun .com

May 18 '06 #4
Michael B Allen <mb*****@ioplex .com> writes:
I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?


Marc Thrun already answered your question, but I have a question for you:

Why do you want to do this? Your type already has a perfectly good
name, "struct foo". What do you want another name for the same thing?

If your answer is "to save typing", that's not a good reason.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 18 '06 #5
Michael B Allen schrieb:
I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?

Thanks,
Mike


You have to use the full type identifier, i.e.

typedef struct foo foo_t;

in your case. This will create an alias for struct foo called foo_t.


But unlike a forward reference this is a type definition. And because I
need it in multiple header files I will start to get redefinition errors.

sounds like you really want this structure in a seperate header
included by the others and properly protected, something like a
common_types.h.

May 19 '06 #6

Michael B Allen wrote:
On Thu, 18 May 2006 23:43:06 +0200, Marc Thrun wrote:
Michael B Allen schrieb:
<snip> foo_t;
typedef foo_t;
typedef struct foo_t;
<snip>

But unlike a forward reference this is a type definition. And because I
need it in multiple header files I will start to get redefinition errors.

I'm toast.


Why do you need it in multiple header files? Even if the files are part
of separate projects and you find out that you need to use two file
headers in the same project, you can use #ifndef in the header files.
The problem will appear when the structure is different from file to
file while retaining the same name.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

May 19 '06 #7
On Thu, 18 May 2006 21:48:51 +0000, Keith Thompson wrote:
Michael B Allen <mb*****@ioplex .com> writes:
I have a forward reference like:

struct foo;

int some_fn(struct foo *param);

Because the parameter is a pointer the compiler is satisfied.

But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The
following all fail to compile:

foo_t;
typedef foo_t;
typedef struct foo_t;

Is there any way to declare a forward refernce for a tyepdef'd type?


Marc Thrun already answered your question, but I have a question for you:

Why do you want to do this? Your type already has a perfectly good
name, "struct foo". What do you want another name for the same thing?

If your answer is "to save typing", that's not a good reason.


Actually that's not why. In fact I don't like typedefs. They hide far too
much. Is it a pointer? Is it a structure or an integer? Bah.

The reason I need a typedef here is because I'm dynamically generating
code and the first parameter of the generated functions is a 'user
defined type' specified to the generator. Currently I'm using a standard
struct type:

struct user_defined_ty pe;
int dynamic_fn(stru ct user_defined_ty pe *p1);

But frequently an 'existing type' unknown to the generated code would
be suitable to use directly as the 'user defined type'. Therefore I
would like it to be a typedef so that the user can define it as the
'existing type' using a simple typedef:

typedef struct existing_type user_defined_ty pe_t;

and then somehow forward reference the 'user defined type':

typedef user_defined_ty pe_t; /* forward reference */
int dynamic_fn(user _defined_type_t *p1);

I suppose the answer is to either pass the 'existing type' to the
generator or embed it in another struct that can be forward referenced.

Mike

May 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2858
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward reference of a data structure ? For a class, I just say: class SomeClass; "struct SomeStructure;" does not work to forward declare the data...
2
4497
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying to gather various different classes into a common namespace using typedefs: class QWidget {}; class MyListview {}; namespace gui
23
3822
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
2
508
by: Carlos Martinez Garcia | last post by:
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 {
7
5975
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
3
4096
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i dont have any issues. but according to some norms in the review I am not supposed to use this file direclty in the header file .. but its fine if i...
8
28439
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
5
6981
by: aaragon | last post by:
Hello, I was wondering if it is possible to forward declare a type definition. If so, what is the way to do it? I found a couple of messages where they say it's not possible but there must be a way to do it. Thank you. a²
11
8296
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.