Native Arrays in Visual Studio 2010

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?
You must have some other syntax error. That construct is fine. Maybe if you posted more of the code we could be more helpful.
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
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
Topic archived. No new replies allowed.