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? 5 1927
On 19 Jul, 12:07, fmas...@gmail.com 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.
On 19 Lug, 13:46, gw7...@aol.com wrote:
On 19 Jul, 12:07, fmas...@gmail.com 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?
On Jul 19, 5:00*pm, fmas...@gmail.com 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)
On 19 Jul, 13:00, fmas...@gmail.com 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. fm*****@gmail.com 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
by: Randy Yates |
last post by:
I'm having a problem with forward references.
For example,
class DATE;
class MYCLASS;
class MYCLASS
{
public:
|
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
|
by: Milind |
last post by:
Hi,
I was trying to implement a composition relation, somthing of the
following type:
class A
{
public:
class B
{
|
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 .
| |
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?
...
|
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 {
|
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>
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |