473,653 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

forward declarations and incomplete types

Hello,
I'm have a question about forward structure declarations and
incomplete types. The complete structure of my file is pretty complex,
but just as an example the code below will explain the problem:

struct b;
struct a {
struct b m;
};
struct b {
struct a m;
};

This code won't compile: my compiler (gcc) says that a.m "has
incomplete type". Changing the members' types to structure pointers
works, but it will be nicer if I could do this as if the two
definitions were not "cross-defined".
Is there a way for doing this?
Jul 19 '08 #1
5 1971
On 19 Jul, 12:07, fmas...@gmail.c om wrote:
Hello,
I'm have a question about forward structure declarations and
incomplete types. The complete structure of my file is pretty complex,
but just as an example the code below will explain the problem:

struct b;
struct a {
* * struct b m;};

struct b {
* * struct a m;

};

This code won't compile: my compiler (gcc) says that a.m "has
incomplete type". Changing the members' types to structure pointers
works, but it will be nicer if I could do this as if the two
definitions were not "cross-defined".
Is there a way for doing this?
I thought this was a FAQ, but I can't actually find it...

Your problem is that you want a to contain a b, and b to contain an a.
In fact, the way you have written it, the two structs look identical,
and there don't seem to be any actual members there that you can use.
But if there were, then you would be wanting each struct to be bigger
than the other. Obviously this is impossible.

You probably want at least one of the structs (probably both) to
contain a pointer to a struct of the other sort. If this does not seem
right, feel free to come back with more details.

Hope that helps.
Paul.
Jul 19 '08 #2
On 19 Lug, 13:46, gw7...@aol.com wrote:
On 19 Jul, 12:07, fmas...@gmail.c om wrote:
Hello,
I'm have a question about forward structure declarations and
incomplete types. The complete structure of my file is pretty complex,
but just as an example the code below will explain the problem:
struct b;
struct a {
struct b m;};
struct b {
struct a m;
};
This code won't compile: my compiler (gcc) says that a.m "has
incomplete type". Changing the members' types to structure pointers
works, but it will be nicer if I could do this as if the two
definitions were not "cross-defined".
Is there a way for doing this?

I thought this was a FAQ, but I can't actually find it...

Your problem is that you want a to contain a b, and b to contain an a.
In fact, the way you have written it, the two structs look identical,
and there don't seem to be any actual members there that you can use.
But if there were, then you would be wanting each struct to be bigger
than the other. Obviously this is impossible.

You probably want at least one of the structs (probably both) to
contain a pointer to a struct of the other sort. If this does not seem
right, feel free to come back with more details.

Hope that helps.
Paul.
Ok, one problem is actually caused by my stupidity :) Of course I
can't have them the way I wrote. At this point the "incomplete type"
problem remains but it's easy to solve moving the code (I added some
other members just to make it more realistic).

struct b;
struct a {
int i;
struct b m;
};
struct b {
int i, j;
struct a *m;
};

This will continue not to work. But I don't think there's a way to get
around it without moving things around, isn't it?
Jul 19 '08 #3
On Jul 19, 5:00*pm, fmas...@gmail.c om wrote:

struct b;
struct a {
* * int i;
* * struct b m;};
No work arounds for this are possible; you need to have a pointer to
the incomplete type. The definition reserves space and in this case,
it can't reserve memory as the compiler is not aware about the amount
of space.
>
struct b {
* * int i, j;
* * struct a *m;
Need not have a pointer(until and unless you really need it to be a
pointer; i.e not required by the compiler)

Jul 19 '08 #4
On 19 Jul, 13:00, fmas...@gmail.c om wrote:
struct b;
struct a {
* * int i;
* * struct b m;};

struct b {
* * int i, j;
* * struct a *m;

};

This will continue not to work. But I don't think there's a way to get
around it without moving things around, isn't it?
No, that won't work, because if you're going to include a whole b as
part of a, it needs to know how big b is. So you need to tell it that
first.

But your code should work if you move things around, as follows:

struct a;

struct b {
int i, j;
struct a *m;
};

// no problem here, we just want a pointer to a

struct a {
int i;
struct b m;};

// no problem here, we now know how big a b is.

Paul.
Jul 19 '08 #5
fm*****@gmail.c om wrote:
Hello,
I'm have a question about forward structure declarations and
incomplete types. The complete structure of my file is pretty complex,
but just as an example the code below will explain the problem:

struct b;
struct a {
struct b m;
};
struct b {
struct a m;
};
This involves infinitely large memory for even a single instance of
either struct. Be glad your compiler rejects it.
Jul 19 '08 #6

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

Similar topics

11
37067
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't explicitly say about enum's forward declaration, but one example shows it in 18.2.1, clause 4:
11
2760
by: Randy Yates | last post by:
I'm having a problem with forward references. For example, class DATE; class MYCLASS; class MYCLASS { public:
11
2427
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
11
2804
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
9
1999
by: vishnu | last post by:
what is the exact difference between including a class header file and forward declaration. and Is there a case , where in forward declaration is not possible and including is .
23
3841
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 {
11
2594
by: Martin Eisenberg | last post by:
Hi Antoine, just redirecting you... Antoine Trux wrote: > Hi, > > Is the following code legal: > > ------> code starts here <------ > #include <stddef.h>
11
8310
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
8370
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
8283
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
8811
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8470
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8590
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...
0
7302
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4147
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...
1
2707
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
2
1591
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.