error: braces around scalar initializer

Discussion to talk about software related topics only.
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

error: braces around scalar initializer

Post by seulater »

Is there somthing i am not doing right with the code below ?
i get a "error: braces around scalar initializer for type 'BYTE'" error message.

Code: Select all

BYTE Font_12[] = 
{

/* Element 0x0000 - Char 0x0000 */
{{10, 12},{
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x7F,0x00,  /*  .%%%%%%%........  */
0x41,0x00,  /*  .%.....%........  */
0x41,0x00,  /*  .%.....%........  */
0x41,0x00,  /*  .%.....%........  */
0x41,0x00,  /*  .%.....%........  */
0x41,0x00,  /*  .%.....%........  */
0x7F,0x00,  /*  .%%%%%%%........  */
0x00,0x00,  /*  ................  */
0x00,0x00   /*  ................  */
}}
};
User avatar
pbreed
Posts: 1091
Joined: Thu Apr 24, 2008 3:58 pm

Re: error: braces around scalar initializer

Post by pbreed »

I don't think that will compile ANY where.
It looks like you copied from a place that was initializing some kind of class or structure.

Shouldn't it be:
BYTE Font_12[] =
{
/* Element 0x0000 - Char 0x0000 */
10, 12
0x00,0x00, /* ................ */
0x00,0x00, /* ................ */
0x00,0x00, /* ................ */
0x7F,0x00, /* .%%%%%%%........ */
0x41,0x00, /* .%.....%........ */
0x41,0x00, /* .%.....%........ */
0x41,0x00, /* .%.....%........ */
0x41,0x00, /* .%.....%........ */
0x41,0x00, /* .%.....%........ */
0x7F,0x00, /* .%%%%%%%........ */
0x00,0x00, /* ................ */
0x00,0x00 /* ................ */
};
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: error: braces around scalar initializer

Post by seulater »

Actually the only difference was that the original looks like this.
sUnit Font_12[256]

I have copied the original file.
I did some searching on "braces around scalar initializer" and found this link

http://cboard.cprogramming.com/cplusplu ... lizer.html

I know its not exactly the same, but it looks like the principal of it is.
Which is why i am curious why this attached file does not work.
Attachments
Font_12.h
(128.77 KiB) Downloaded 423 times
greengene
Posts: 164
Joined: Wed May 14, 2008 11:20 am
Location: Lakeside, CA

Re: error: braces around scalar initializer

Post by greengene »

paul's comment is right on the money - gasp!

from your font_12.h file:
sUnit Font_12[256] =
{

/* Element 0x0000 - Char 0x0000 */
{{10, 12},{
<snip>

sUnit appears to be a structure not simple BYTEs.
so, figure out what it is that you need. if it is the
structure sUnit then the first part of the initialization
is probably needed {10,12}.
if you don't need that info and are just redefining the
data, then remove the line or the braces but i don't
think that is what you want.
so, check out what sUnit is and adjust your code
accordingly.
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: error: braces around scalar initializer

Post by seulater »

Can someone give a bit of structure help.


this is the data table that i need to implement.

Code: Select all

FONT14 style[3] =
{

/* Element 0x0000 - Char 0x0023 */
{{11, 14},{
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x09,0x00,  /*  ....%..%........  */
0x09,0x00,  /*  ....%..%........  */
0x12,0x00,  /*  ...%..%.........  */
0x3F,0x80,  /*  ..%%%%%%%.......  */
0x12,0x00,  /*  ...%..%.........  */
0x12,0x00,  /*  ...%..%.........  */
0x7F,0x00,  /*  .%%%%%%%........  */
0x24,0x00,  /*  ..%..%..........  */
0x24,0x00,  /*  ..%..%..........  */
0x00,0x00,  /*  ................  */
0x00,0x00   /*  ................  */
}}
,

/* Element 0x0001 - Char 0x0024 */
{{9, 14},{
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x10,0x00,  /*  ...%............  */
0x10,0x00,  /*  ...%............  */
0x3E,0x00,  /*  ..%%%%%.........  */
0x50,0x00,  /*  .%.%............  */
0x50,0x00,  /*  .%.%............  */
0x30,0x00,  /*  ..%%............  */
0x1C,0x00,  /*  ...%%%..........  */
0x12,0x00,  /*  ...%..%.........  */
0x12,0x00,  /*  ...%..%.........  */
0x7C,0x00,  /*  .%%%%%..........  */
0x10,0x00,  /*  ...%............  */
0x10,0x00   /*  ...%............  */
}}
,

/* Element 0x0002 - Char 0x0025 */
{{14, 14},{
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x00,0x00,  /*  ................  */
0x30,0x80,  /*  ..%%....%.......  */
0x49,0x00,  /*  .%..%..%........  */
0x49,0x00,  /*  .%..%..%........  */
0x4A,0x00,  /*  .%..%.%.........  */
0x32,0x60,  /*  ..%%..%..%%.....  */
0x02,0x90,  /*  ......%.%..%....  */
0x04,0x90,  /*  .....%..%..%....  */
0x04,0x90,  /*  .....%..%..%....  */
0x08,0x60,  /*  ....%....%%.....  */
0x00,0x00,  /*  ................  */
0x00,0x00   /*  ................  */
}}
};



What i did was this,

typedef struct font14{
BYTE xandy[2];
BYTE fontdata[3 * 14];
}FONT14;


yet, when i try "printf("x=%d\r\n",style[0].x);" i get a warning of format '%d' expects type 'int', but argument 2 has type 'BYTE*'


Ideally what i am looking for is a few things, but i dont know if its possible.

#1) the first 2 bytes in each element is the x & y size in pixels.
i would like to be able to use something like:
x = FONT14[x].xSize
y = FONT14[x].ySize
and for the actual data itself, use FONT14[x].data, or even a pointer to FONT14[x].data
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: error: braces around scalar initializer

Post by tod »

struct XySize
{
BYTE xSize;
BYTE ySize;
}

struct Font14
{
XySize sizeInfo;
BYTE fontData[3*14]; //Don't use these magic numbers though - define what they are in constants and use the constants
}

Font14 style[3] =...your data...

then
BYTE x_size = style[0].sizeInfo.xSize;
BYTE y_size = sytle[0].sizeInfo.ySize;
BYTE* font_data = style[0].fontData;
BYTE one_bye_of_font_data = style[0].fontData[x];

If you want to printf a BYTE as an int just (int) cast it. Better yet use cout and you won't get the error, although since BYTE is probably defined as unsigned char 0 you won't get the value you want unless you cout<< x_size +0;

I'm just typing this directly into the forum, haven't compiled it or anything so consider it pseudo-code.
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: error: braces around scalar initializer

Post by seulater »

Thanks, when i compile it i get a "error: expected initializer before 'style'" @ "Font14 style[3] "

I changed it to this and now it work, but just checking if its ok.

struct XySize
{
BYTE xSize;
BYTE ySize;
};

struct FONT12F
{
XySize sizeInfo;

BYTE data[95];
}

FONT12F[95]=...your data...
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: error: braces around scalar initializer

Post by tod »

Nope, other than a couple of missing ;s, and misspelling the word "style" it should work as advertised.
Your line
FONT12F[95]=...your data
Should give you an error, FONT12F is a type name, you can't declare an array of a typename.


Here's a complete, working example.

Code: Select all



#include <iostream>
#include <iomanip>
using namespace std;


typedef unsigned char BYTE;
struct XySize
{
		BYTE xSize;
		BYTE ySize;
};

struct Font14
{
		XySize sizeInfo;
		BYTE fontData[3*14]; //Don't use these magic numbers though - define what they are in constants and use the constants
} ;



void testStruct()
{
	Font14 style[3]=
	{

	/* Element 0x0000 - Char 0x0023 */
	{{11, 14},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x09,0x00,  /*  ....%..%........  */
	0x09,0x00,  /*  ....%..%........  */
	0x12,0x00,  /*  ...%..%.........  */
	0x3F,0x80,  /*  ..%%%%%%%.......  */
	0x12,0x00,  /*  ...%..%.........  */
	0x12,0x00,  /*  ...%..%.........  */
	0x7F,0x00,  /*  .%%%%%%%........  */
	0x24,0x00,  /*  ..%..%..........  */
	0x24,0x00,  /*  ..%..%..........  */
	0x00,0x00,  /*  ................  */
	0x00,0x00   /*  ................  */
	}}
	,

	/* Element 0x0001 - Char 0x0024 */
	{{9, 14},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x10,0x00,  /*  ...%............  */
	0x10,0x00,  /*  ...%............  */
	0x3E,0x00,  /*  ..%%%%%.........  */
	0x50,0x00,  /*  .%.%............  */
	0x50,0x00,  /*  .%.%............  */
	0x30,0x00,  /*  ..%%............  */
	0x1C,0x00,  /*  ...%%%..........  */
	0x12,0x00,  /*  ...%..%.........  */
	0x12,0x00,  /*  ...%..%.........  */
	0x7C,0x00,  /*  .%%%%%..........  */
	0x10,0x00,  /*  ...%............  */
	0x10,0x00   /*  ...%............  */
	}}
	,

	/* Element 0x0002 - Char 0x0025 */
	{{14, 14},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x30,0x80,  /*  ..%%....%.......  */
	0x49,0x00,  /*  .%..%..%........  */
	0x49,0x00,  /*  .%..%..%........  */
	0x4A,0x00,  /*  .%..%.%.........  */
	0x32,0x60,  /*  ..%%..%..%%.....  */
	0x02,0x90,  /*  ......%.%..%....  */
	0x04,0x90,  /*  .....%..%..%....  */
	0x04,0x90,  /*  .....%..%..%....  */
	0x08,0x60,  /*  ....%....%%.....  */
	0x00,0x00,  /*  ................  */
	0x00,0x00   /*  ................  */
	}}
	};

	for (int i = 0; i< 3; ++i)
	{
		BYTE x_size = style[i].sizeInfo.xSize;
		BYTE y_size = style[i].sizeInfo.ySize;
		BYTE* font_data = style[i].fontData;
		BYTE one_byte = style[i].fontData[10];

		cout << "x_size:"<< x_size+0 <<" y_size:"<< y_size+0 << " font data byte:0x" << hex<< one_byte+0  <<dec <<endl;

		font_data += 10;
		cout << "font data from ptr 0x" <<hex <<(*font_data)+0 <<" : "<< dec << endl;

	}
}

int main()
{

	testStruct();
}

Sample Output
x_size:11 y_size:14 font data byte:0x12
font data from ptr 0x12 :
x_size:9 y_size:14 font data byte:0x50
font data from ptr 0x50 :
x_size:14 y_size:14 font data byte:0x49
font data from ptr 0x49 :
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: error: braces around scalar initializer

Post by seulater »

Thanks, I had to stray from your example for the following reasons, again i dont know if it's correct but its working.

#1) i needed to add the static, because i have 24 font types. so i have each font type in its own .h file and all 24 .h files in their own folder called "Fonts" then i just #include each one of these files regardless if i use them or not. If i don't use them the compiler does not bring them in so i never have to worry about making sure i included this one or that.

when i used Font14 style[3]= the name "style" caused naming problems in the rest of the font files. when i added the static in there, it allowed me to use the "Font12T[4] =". i could drop the "style" name altogether, so that helped there.
But dont know if its smart to do that.






Code: Select all

struct Font12T_XySize
{
	BYTE xSize;
	BYTE ySize;
};

static struct Font12T
{
	Font12T_XySize sizeInfo;
	BYTE fontdata[24];
}

Font12T[4] =
{

	/* Element 0x0010 - Char 0x0030 */
	{{8, 12},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x38,0x00,  /*  ..%%%...........  */
	0x44,0x00,  /*  .%...%..........  */
	0x44,0x00,  /*  .%...%..........  */
	0x44,0x00,  /*  .%...%..........  */
	0x44,0x00,  /*  .%...%..........  */
	0x44,0x00,  /*  .%...%..........  */
	0x38,0x00,  /*  ..%%%...........  */
	0x00,0x00,  /*  ................  */
	0x00,0x00   /*  ................  */
	}}
	,

	/* Element 0x0011 - Char 0x0031 */
	{{8, 12},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x10,0x00,  /*  ...%............  */
	0x30,0x00,  /*  ..%%............  */
	0x10,0x00,  /*  ...%............  */
	0x10,0x00,  /*  ...%............  */
	0x10,0x00,  /*  ...%............  */
	0x10,0x00,  /*  ...%............  */
	0x38,0x00,  /*  ..%%%...........  */
	0x00,0x00,  /*  ................  */
	0x00,0x00   /*  ................  */
	}}
	,

	/* Element 0x0012 - Char 0x0032 */
	{{8, 12},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x38,0x00,  /*  ..%%%...........  */
	0x44,0x00,  /*  .%...%..........  */
	0x04,0x00,  /*  .....%..........  */
	0x08,0x00,  /*  ....%...........  */
	0x10,0x00,  /*  ...%............  */
	0x20,0x00,  /*  ..%.............  */
	0x7C,0x00,  /*  .%%%%%..........  */
	0x00,0x00,  /*  ................  */
	0x00,0x00   /*  ................  */
	}}
	,

	/* Element 0x0013 - Char 0x0033 */
	{{8, 12},{
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x00,0x00,  /*  ................  */
	0x38,0x00,  /*  ..%%%...........  */
	0x44,0x00,  /*  .%...%..........  */
	0x04,0x00,  /*  .....%..........  */
	0x18,0x00,  /*  ...%%...........  */
	0x04,0x00,  /*  .....%..........  */
	0x44,0x00,  /*  .%...%..........  */
	0x38,0x00,  /*  ..%%%...........  */
	0x00,0x00,  /*  ................  */
	0x00,0x00   /*  ................  */
	}}
};


    BYTE x_size = Font12T[0].sizeInfo.xSize;
    BYTE y_size = Font12T[0].sizeInfo.xSize;
    BYTE* font_data = Font12T[0].fontdata;

    iprintf("x[%d],y[%d]\r\n",x_size,y_size);


User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: error: braces around scalar initializer

Post by tod »

Now I see. First of all I assume you're going through all this because you're required to use these initializer lists you been given. Otherwise I would just create a Font class that uses a vector<BYTE> for the font data and create a named instance of each Font I needed. It would be much much cleaner. But assuming you can't, the final approach becomes more a matter of style and possibly "best practices" than anything else. I don't see anything wrong with what you are doing. Here's what Stroustrup has to say on the matter:
In C and older C++ programs, the keyword static is (confusingly) used to mean "use internal linkage". Don't use static except inside functions and classes.
- p 200 The C++ Programming Language

He references sections that show the standard use of a static local variable in a function and a static member variable for a class.

Honestly, I wasn't even aware you could do you what you did so I found it very confusing. In effect you are declaring an instance (named Font14) of an anonymous structure. Putting that static keyword there changes Font14 from being a typename to being an instance name. You can still get what you want a couple of ways without the static keyword. You could just drop the static so that Font14 is still a typename and then you declare a unique "style" variable (styleFont14 perhaps) in each header file. That would be the smallest change from what you have. My preferred approach would be to use namespaces. The definition for struct XySize would go in one and only one header that all the other headers would include. Every other header would start with a unique namespace name so then every header could use the same names. The only things that would change would be the size of the array and of course the data in the initializer list.

Code: Select all

namespace Font14{
		
	struct Font
	{
		XySize sizeInfo;
		BYTE fontData[3*14]; 
	};

	Font style[3]=...your data here...
}
Then in the code that uses fonts you can either prefix the code with a

Code: Select all

using namespace Font14;
BYTE x_size = style[i].sizeInfo.xSize;
//... work with font 14
using namespace Font16;
//...work with Font 16 
or if you interleave font calls you can just add the qualifer each time

Code: Select all

Byte x_size = Font14::style[i].sizeInfo.xSize;
Byte some_data = Font16::style[i].fontData[10];
Post Reply