LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2008, 03:15 AM   #1
duduarbel
LQ Newbie
 
Registered: May 2008
Posts: 4

Rep: Reputation: 0
error: array type has incomplete element type


Code that used to compile will not compile in Fedora.
it gives error: 'array type has incomplete element type' on the first line in file A.h.
I've understood why, but still I have no workaround.
How can I fix the following code with minimum changes?

file: A.c
#include B.h
#include A.h

struct B a[3];
...

file A.h
extern struct B a[3];
...

HELP PLEASE!

Tx,
Dudu Arbel
 
Old 05-26-2008, 06:50 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
It sounds like 'struct B' is not defined anywhere - did you define it in b.h?
 
Old 05-26-2008, 06:55 AM   #3
duduarbel
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Yes - B is a simple struct defined at B.h
 
Old 05-26-2008, 07:42 AM   #4
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Do you really need the extern array in A.h ? If not just leave it in the c file and remove it from the header file. Otherwise, you're going to need knowledge about B inside the A.h file (include or forward declaration).
 
Old 05-26-2008, 09:45 AM   #5
duduarbel
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
I want to use 'a' variable inside file A.h, hence I must have some kind of extern to it, no?
Adding forward declaration does not help:

file A.h
struct B;
extern struct B a[3];
...
 
Old 05-26-2008, 11:25 AM   #6
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
Quote:
Originally Posted by duduarbel View Post
I want to use 'a' variable inside file A.h, hence I must have some kind of extern to it, no?
Adding forward declaration does not help:

file A.h
struct B;
extern struct B a[3];
...

Try including the file B.h in A.h
 
Old 05-27-2008, 06:20 AM   #7
duduarbel
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry to say but you're missing the point.
The new gcc compiler refuses to compile lines like:
extern struct B a[3];

I have found lots of information and explanations why this line is illegal, and I'm cool with that.

My question is: How can I change this code, so it will compile?
One possible solution is to change the definition of a:
struct B *a = new B[3];

extern B *a; // This is ok

The above code is legal, but this change is unexceptable.
Any other ideas?
 
Old 05-27-2008, 06:34 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Adding forward declaration does not help:

file A.h
struct B;
extern struct B a[3];
...
extern B *a; // This is ok
The problem is that the type of the structure is not known and therefore a forward declaration of the type will not work unless the instance is a reference, in C a pointer and in C++ also a reference.

"The above code is legal, but this change is unexceptable." If you can not include the definition of the type in the header the normal path to take is to use the pimpl idiom or a reference. The fault maybe from your design if this is your code, I do not know it is but will assume so and assume C++ as you are using new.
Why does your design require a global instance of an array?
Why an array and not a vector?
Why not local or in a namespace other than global?
Why does the definition need to be made available to header which can not use it or operate on the type?

Last edited by dmail; 05-27-2008 at 06:49 AM.
 
Old 05-27-2008, 12:25 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by duduarbel View Post
The new gcc compiler refuses to compile lines like:
extern struct B a[3];
How new? This seems to work fine on my version of gcc. As long as you include B.h before declaring the array I don't see any reason why it shouldn't work. In this code it's done by including B.h before A.h in the c file which is probably not a great idea, but the compiler doesn't care.

Code:
~/tmp/struct$ gcc --version
gcc (GCC) 4.0.2
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

~/tmp/struct$ cat a.c
#include "B.h"
#include "A.h"

struct B a[3];

int main () {
    return 0;
}
~/tmp/struct$ cat A.h
extern struct B a[3];
~/tmp/struct$ cat B.h
struct B {
    int field;
};
~/tmp/struct$ make a
gcc -Wall -ansi -pedantic    a.c   -o a
~/tmp/struct$ ./a ; echo $?
0

Last edited by ntubski; 05-27-2008 at 12:26 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error: array type has incomplete element type nasim751 Linux - Software 1 04-14-2008 09:02 PM
Array type has incomplete type gopal229 Programming 3 10-16-2007 12:23 AM
Array type has incomplete element type Emsie Linux - Software 9 01-04-2007 06:59 AM
"array type has incomplete element type" compiling 2.4.32 kernel Akabaka Linux - General 1 01-08-2006 12:48 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration