473,734 Members | 2,698 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 15287
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
2869
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 structure. This is the structure that I am trying to generate a vector of pointers to:
2
4518
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
3853
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
5998
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
4104
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 use that inside the corresponding file. To avoid compiler error, i forward declared the structure...
8
28464
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
7007
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
8326
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
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8777
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6737
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6033
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.