Dec 14, 2010 at 11:38am UTC
I see this used all over the place on the net...
unsigned char array[8]={1,2,3,4,5,6,7,8};
Why do I get ...
error C2059: syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
...when I try it?
Dec 14, 2010 at 1:08pm UTC
You must have some other syntax error. That construct is fine. Maybe if you posted more of the code we could be more helpful.
Dec 14, 2010 at 1:26pm UTC
I finally figured out what it was (thanks a little to advice from MSDN)...
in my header I now have
1 2 3 4 5 6
class MyClass
{
static const unsigned char array[];
MyClass();
~MyClass();
};
and in the other code file
1 2 3
const unsigned char MyClass::array[]={1,2,3,4,5,6,7,8};
MyClass::MyClass(){}
MyClass::~MyClass(){}
Sorry my original code was unclear - and my question not obvious, I was more just wanting to know why it would not work, seems that you can't declare arrays like that in header files....
Last edited on Dec 14, 2010 at 1:27pm UTC
Dec 14, 2010 at 3:07pm UTC
static const unsigned char array[]
is an incomplete type and its size is mostly unknown. It's only known in the module that declares array
.
You have to be careful with such declarations.
Last edited on Dec 14, 2010 at 3:08pm UTC