system
March 25, 2011, 4:56am
1
Hello,
I'm trying to make an array of structs. Here's my code:
typedef struct
{
int r;
int g;
int b;
long code;
} Color;
#define NUM_COLORS 3
Color colors[NUM_COLORS];
colors[0].r = 255; //red
colors[0].g = 0;
colors[0].b = 0;
colors[0].code = 0x2FD807F;
colors[1].r = 0; //green
colors[1].g = 255;
colors[1].b = 0;
colors[1].code = 0x2FD40BF;
colors[2].r = 0; //blue
colors[2].g = 0;
colors[2].b = 255;
colors[2].code = 0xFDC03F;
I keep getting the error "expected constructor, destructor, or type conversion before ''." token."
Can someone please tell me what silly mistake I'm making....?
Thanks!
system
March 25, 2011, 5:04am
2
is all of that code within a function? or outside?
system
March 25, 2011, 5:08am
3
it's all at the very top, before setup().
I actually had the typedef in a header file, but condensed it here for ease of posting...
system
March 25, 2011, 5:57am
4
those statements must be inside a function
or alternatively
Color colors[NUM_COLORS] = {
{ // [0]
.r = 255,
.g = 0,
.b = 0,
.code = 0x2FD807F,
},
{ // [1]
.r = 0,
.g = 255,
.b = 0,
.code = 0x2FD40BF,
},
{ // [2]
.r = 0,
.g = 0,
.b = 255,
.code = 0xFDC03F,
},
};
It compiles OK when in a function:
typedef struct
{
int r;
int g;
int b;
long code;
} Color;
#define NUM_COLORS 3
Color colors[NUM_COLORS];
void setup ()
{
colors[0].r = 255; //red
colors[0].g = 0;
colors[0].b = 0;
colors[0].code = 0x2FD807F;
colors[1].r = 0; //green
colors[1].g = 255;
colors[1].b = 0;
colors[1].code = 0x2FD40BF;
colors[2].r = 0; //blue
colors[2].g = 0;
colors[2].b = 255;
colors[2].code = 0xFDC03F;
}
void loop () {}
system
March 25, 2011, 6:35am
6
If I do it the way frank suggested, I get an "expected primary-expression before '.' token" complaint....
void setup()
{
#define NUM_COLORS 3
Color colors[NUM_COLORS] = {
{ // red
.r = 255,
.g = 0,
.b = 0,
.code = 0x2FD807F,
},
{ // green
.r = 0,
.g = 255,
.b = 0,
.code = 0x2FD40BF,
},
{ // blue
.r = 0,
.g = 0,
.b = 255,
.code = 0xFDC03F,
},
};
....rest of setup()
Thanks for the help so far....
Didn't like my first idea, huh? Well this works outside a function ...
typedef struct
{
int r;
int g;
int b;
long code;
} Color;
#define NUM_COLORS 3
Color colors[NUM_COLORS] = {
{ 255, 0, 0, 0x2FD807F },
{ 0, 255, 0, 0x2FD40BF },
{ 0, 0, 255, 0xFDC03F },
};
void setup () {}
void loop () {}
system
March 25, 2011, 6:40am
8
my bad, my syntax apparently doesn't work in C++, but it does in C
system
March 25, 2011, 6:50am
9
Sorry Nick,
I actually got it to work your way, but tried it Frank's way too and was wondering why one was working and the other wasn't! Thanks for the notation on how to do it outside a function....
Thanks to both of you for the tips!