Why does the following code generate a compile error?
Edit: My original code wasn't clear - I've split the code up into separate files...
First.h
class First
{
public:
static const char* TEST[];
public:
First();
};
First.cpp
const char* First::TEST[] = {"1234", "5678"};
First::First()
{
uint32_t len = sizeof(TEST); // fine
}
Determining the size within the First
class seems fine, however...
Second.h
class Second
{
public:
Second();
};
Second.cpp
#include "First.h"
Second::Second()
{
uint32_t len = sizeof(First::TEST); // error
uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
}
I get the following error: 'const char *[]': illegal sizeof operand
sizeof only works on complete types. const char* TEST[]
is not a complete type until it is defined in First.cpp.
sizeof(char*[10]) == sizeof(char*) * 10 == 40
sizeof(short[10]) == sizeof(short) * 10 == 20
// a class Foo will be declared
class Foo;
sizeof(Foo) == //we don't know yet
// an array bar will be defined.
int bar[];
sizeof(bar) == sizeof(int) * ? == //we don't know yet.
// actually define bar
int bar[/*compiler please fill this in*/] = { 1, 2, 3 };
sizeof(bar) == sizeof(int) * 3 == 12
// note bar is exactly the right size
// an array baz is defined.
int baz[4];
sizeof(baz) == sizeof(int) * 4 == 16
// initialize baz
int baz[4] = { 1, 2, 3 };
sizeof(bar) == sizeof(int) * 4 == 16
// note baz is still 4 big, the compiler doesn't control its size
To get this to work as you wish, you can:
First::TEST
array to its declaration (static const char* TEST[2];
)First::TEST
. The method cannot be inline, it would have to be defined in First.cpp.Primarily because the compilation of First.cpp and Second.cpp are independent of each other.
sizeof() in Second is (generally) resolved at compile time, when only the array's declaration is known, and space for the static hasn't been allocated cannot be calculated. See http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With