I have the following souce about structure which compiled ok in gcc 4.4.6 :
struct st1
{
char name[12];
int heartbeat ;
double price ;
int iFlag ;
} ;
struct st2 {
struct st1 ;
char pad[64 - sizeof(struct st1)] ;
} __attribute__((aligned(64))) ;
int main (void)
{
printf("length of struct st2=(%d)\n",sizeof(struct st2) ) ;
}
gcc -fms-extensions test1.c -o test1.exe
./test1.exe ===> length of struct st2=(64)
I copy test1.c to test1.cpp and try to compile as :
g++ -fms-extensions test1.cpp -o test1.exe
and then I got :
test1.cpp:23: error: invalid application of sizeof to incomplete type st2::st1
I know this error showes char pad[64 - sizeof(struct st1)] ; does not work in g++ , although it works in gcc , if I like this works in g++ , what can I do ?
In your struct st2:
struct st1 ;
This is a forward declaration of a struct st1
.
Here, you are basically saying to your compiler : "Hey, there's a struct st1
in the namespace of the struct st2
(hence the st2::st1
), but I'm not going to give you its properties !"
Since you are not providing its properties, the compiler will raise an "incomplete type" error : it can't know about the size of this type, and therefore cannot resolve your sizeof
.
If you want to have an instance of your struct st1
in your struct st2
, you should write:
struct st1 my_variable_name;
This will effectively add an instance of your struct st1
in your struct st2
.
If you DON'T want an instance of your struct st1
in your struct st2
, just remove this line - your compiler already knows about the struct st1
, since it's declared right above.