Need help with structs ?

Hello again.. I have defined these 2 struct, and when I try to compile it gives me two errors, of course it is the same error:

||=== Build: Debug in D2LoDRuneWords (compiler: GNU GCC Compiler) ===|
|26|error: too many initializers for 'ITEM'|
|26|error: too many initializers for 'ITEM'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
typedef struct
{
    CHAR Type[ISIZE];
}TYPE;

typedef struct
{
    TYPE weapon;
    TYPE armorhelm;
    TYPE shield;
}ITEM;

ITEM item[] =
{
    {
        {TEXT("Amazon Bow")}, {TEXT("Amazon Spear")}, {TEXT("Assassin Katar")}, {TEXT("Axe")}, {TEXT("Bow")},
        {TEXT("Club")}, {TEXT("Crossbow")}, {TEXT("Dagger")}, {TEXT("Hammer")}, {TEXT("Mace")}, {TEXT("Polearm")},
        {TEXT("Scepter")}, {TEXT("Sorceress Orb")}, {TEXT("Spear")}, {TEXT("Staff")}, {TEXT("Sword")}, {TEXT("Wand")}
    },
    {
        {TEXT("Body Armor")}, {TEXT("Barbarian Helmet")}, {TEXT("Circlet")}, {TEXT("Druid Pelt")}, {TEXT("Helmet")}
    },
    {
        {TEXT("Necromancer Shrunken Head")}, {TEXT("Paladin Shield")}, {TEXT("Shield")}
    },
};


What am I missing ?!
Last edited on
This is just a piece of code.. the rest is not need .. cose my errors just refer here so..
each item has 3 strings.
your first item has like 20 strings.
your second item has 5

like this:
ITEM items[] = {
{TEXT("Sword"), TEXT("Beanie with propeller"), TEXT("Old pot lid")}, //one set of 3 things
...
}

the way you set up ITEM is like for a single character in a game.
The way you tried to use it is like a list of all items in a game.
which one did you want?!

Also you don't need a struct to make a C-string type.
you can just typedef an array of max size directly as a type, without the struct layer.

Last edited on
I'm trying to use
item
in array of chars

 
#define ARRAYSIZE(sel)  (sizeof(sel) / sizeof(sel[0])) 


like this:
1
2
3
4
5
6
7
8
9
            CHAR buff[50];
            memset(&buff, 0, sizeof(buff));   

            for(UINT i = 0; i < ARRAYSIZE(items); i++)
            {
                strncpy(buff, items[i].weapons, sizeof(buff)/sizeof(CHAR));

                SendMessage(combo1, CB_ADDSTRING, 0, (LPARAM)buff);
            }


and then use the struct elements in to a ComboBox... which I already did with other structs, but was just elements of one single struct , not Struct in a struct.

So these items once filled in the ComboBox, choosing one of them
weapons, armors or shields
has to show in a static control. So if I'm choosing for example weapon from the list has to show the first item of the string that has 20 other strings.
Last edited on
Ehh it's a bit complicated .. even if is not seems to be, but yeahh that's what I wanna do.
And it's not ended here because first I just want to make items to appear in a static window just play a bit with the code... but the real goal is that these items has to show further in to a ListBox, where user can select on of them and in base of what type of weapon selects, it shows a combinations of runewords.. and so one.. it's like endless..
Still those two errors put me on the ground.. can't get further
and the Errors come from the first element Type weapon

I know this because I made another try and instead of

1
2
3
4
typedef struct
{
    CHAR Type[ISIZE];
}TYPE;


I ended up with 3 other structs like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Structure Weapons
typedef struct
{
    CHAR Weapon[ISIZE];
}WEAPON;
// Structure Armors Helms
typedef struct
{
    CHAR ArmorHelm[ISIZE];
}ARMORHELM;
// Structure Shields
typedef struct
{
    CHAR Shield[ISIZE];
}SHIELD;


then in the Item struct I just write this :

1
2
3
4
5
6
typedef struct
{
    WEAPON weapon;
    ARMORHELM armorhelm;
    SHIELD shield;
}ITEM;


ISIZE was just defined as 50

Well the error was like this :
|26|error: too many initializers for 'WEAPONS'| and now wear 3 of them not 2 all indicating the same thing. So that's why I'm saying is from the first element of the ITEM struct...
Last edited on
you are writing this like its pure C or 1995. I don't care, but you should at least know the style here is very dated.

here is some random example code that may help you do what you want to do. Or not. I am a little fuzzy on what you want to do. Can you use modern c++, like string and vector and such??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

typedef char cstr[50];

struct ITEM
{
    cstr weapon{};
    cstr armorhelm{};
    cstr shield{};		
};

struct allweapons
{ //example but not useful!
  cstr weaponlist[100];		
};

struct player
{
    int weapon{};
    int armorhelm{};
    int shield{};		
};

int main() 
{
   ITEM i{"spear", "magic helment", "used hubcap"};
   cout << i.weapon << endl;;
   allweapons awlist {"Amazon Bow", "Amazon Spear", "Assassin Katar", "Axe", "Bow",
        "Club", "Crossbow", "Dagger", "Hammer", "Mace", "Polearm",
   "Scepter", "Sorceress Orb", "Spear", "Staff", "Sword", "Wand"};
   cout << awlist.weaponlist[3] << endl;
   
   player bob{8,0,0};
   cout << awlist.weaponlist[bob.weapon]<< endl; //bob has a hammer!
  
   //ok but that is an unholy mess. the struct for all weapons in a list
  // is an unnecessary layer, just do this:
   const cstr allweapons[]{"Amazon Bow", "Amazon Spear", "Assassin Katar", "Axe", "Bow",
        "Club", "Crossbow", "Dagger", "Hammer", "Mace", "Polearm",
   "Scepter", "Sorceress Orb", "Spear", "Staff", "Sword", "Wand"};
   cout << allweapons[bob.weapon] << endl; //bob has a hammer!
//^^^^^^^^^^^^^^^^^see how that is cleaner than first version?
}

you may want to enumerate your lists... so you could say
bob.weapon = hammer; //enum for hammer == 8, etc. And generally clean up magic numbers, which in the example is my fault, you may have already done all that.
Last edited on
I could try your example jonnin and I doubt it is wrong, but I use C language though, I don't know much about C++, and I use it on win32 api project, My program is about a game called Diablo 2 LoD and you may already heard about it, it's an old game, but still played :)
And what do I want to do is just one particular aspect of the game which is to show how the runewords event work in the game and enumerate them as well. So to be short this event is very simple, in game are 33 runes, and items that these runes can be worked with are the ones that I enumerate in my OP. So these items have sockets on it , like 1, 2, 3..max 6
The weapons can have 6 sockets, the armors and the shields can have a maximum 4 sockets, and Helmets can have a maximum 3 sockets. These runes have each of one a different effect that apply when filled in these items, still the game generates as they called RUNEWORDS which is apply on a certain item and a certain combination of these runes. Now what I did until now, I made a ComboBox which you can select the number of sockets so the minimum sockets you may have on an item is at list 2 sockets to make a combination of a RUNEWORD to a maximum 6 and then the last item of the combo is ALL which includes all combination of the 2 ,3 ,4, 5 and 6 sockets. In baze of that if I choose Number socket 2.. in a ListBox that I created appears those RUNEWORDS that works only with 2 socketed items.
Now I need to make another ComboBox that will show my items TYPES not the list , which now will depend on what items I choose from the list + number of sockets to show in my ListBox the list of fthe RUNEWORDS.. It's like a filter when you search something, in a vast list . Now you may ask why did wrote only 3 elements in the struct Weapons/ArmorHelm/Shield, when they are 4 ... this is just because the runes have the same effect on Armor and Helms .. and that's why I wrote as one single Item.
Okay I'm going to try your example. Still what makes me 99% happy and not totally is I'm still thinking on those errors when I wrote the elements of the ITEMS struct, and still can't understand why compiler is complaining about too much initializers when it's clear what I did , and I didn't miss a comma :\... mahh I really wanna know why even there are alternatives like your example..

Still until light comes out .. Cheers and thanks jonnin.. I'll write soon as I make the code working.
ok, you can use C if you want, you had it right with all the typedefs and global variables on the final } of the structs. C++ cleaned that up so they work more like basic types eg int.

as for the error, you have too many, period.

item is a type with 3 of your custom string types inside it. THREE of them.
your attempt to create one has like TWENTY of them. It only holds 3. Twenty is way too many. You are treating your string as if it were and array of strings. It is not, its ONE string. Char is not a string type in C or c++, its one LETTER. your array of them in 'TYPE' is some letters, then -- a word, or a few words with spaces between, etc. But it isnt an array of words or phrases.

the {} is just a shorthand c++ to initialize the variables to be empty. C won't do that part but the syntax isnt the problem. The problem is trying to put so many into 3 locations. Fix that and your code will work -- either only put 3 in each item, or make items hold more!

lets see..
this is sort of like what you have
int x[3] = {1,2,3,4,5}; //no can do

or even
struct three
{
int one,two,three;
};
three x[2] = { {1,2,3,4,5,6,7}, {1,2,3,4,5}}; //you have 2 entries for the array of 'threes' which is correct, but the internal ones try to put 7 and 5 values respectively into a struct that holds 3. This will not work.
this is correct:
three x[2] = { {1,2,3}, {4,5,6}};

edit I just noted the all caps CHAR. what is that? If it is a typedef of arrays of char, then its a new story. If its just a char renamed, what I said holds true.
Last edited on
Ohh my bad I work on win32 Api so when I write for example:

 
LRESULT CALLBACK WndProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


see the UINT is defined unsigned int
soo in winnt.h you have this
typedef char CHAR;
that's why I tape it CHAR
probably is the same thing ?
Well then I have to do this if now I understand well what I was doing.. so now I'll define ITEMS elements as 2D array of strings so now I have this:

1
2
3
4
5
6
7
// Structure Items
typedef struct
{
    TYPE weapon[17][ISIZE];
    TYPE armorhelm[5][ISIZE];
    TYPE shield[3][ISIZE];
}ITEM;


..then this can apply without errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ITEM item[] =
{
    {
        {TEXT("Amazon Bow")}, {TEXT("Amazon Spear")}, {TEXT("Assassin Katar")}, {TEXT("Axe")}, {TEXT("Bow")},
        {TEXT("Club")}, {TEXT("Crossbow")}, {TEXT("Dagger")}, {TEXT("Hammer")}, {TEXT("Mace")}, {TEXT("Polearm")},
        {TEXT("Scepter")}, {TEXT("Sorceress Orb")}, {TEXT("Spear")}, {TEXT("Staff")}, {TEXT("Sword")}, {TEXT("Wand")}
    },
    {
        {TEXT("Body Armor")}, {TEXT("Barbarian Helmet")}, {TEXT("Circlet")}, {TEXT("Druid Pelt")}, {TEXT("Helmet")}
    },
    {
        {TEXT("Necromancer Shrunken Head")}, {TEXT("Paladin Shield")}, {TEXT("Shield")}
    },
};


then this one will not work for this #define ARRAYSIZE(sel) (sizeof(sel) / sizeof(sel[0])) and I have to define another one
Last edited on
..Well now I just tried to do this, and this was that what I had to do from the beginning, and not 2d arrays.

in the ITEM struct was just to write this :

1
2
3
4
5
6
7
// Structure Items
typedef struct
{
    TYPE weapon[ISIZE];
    TYPE armorhelm[ISIZE];
    TYPE shield[ISIZE];
}ITEM;


where ISIZE is :
#define ISIZE 30

And now is working just fine..
Last edited on
yea that works, you made them arrays of strings by adding a dimension.
TYPE weapon[17][ISIZE];
this is now 17 strings, and can hold the info you wanted.
literally it is
an array of 17 char arrays, each char array is a C style string.

computing the size of arrays never appealed to me. You made the array, and it has a fixed size. You know its size. Recomputing it seems weird to me.
Last edited on
Hahaha.. well now that I got it right.. one thing just came up and I still wondering how do I visualize in a ComboBox when you open it from what I did?

Choose Item Types /// <-this is a simple static upon the Combo it does nothing just
/// Info.

Weapons
Armors/Helms
Shields


Well I realize now I'm not smart enough to think so far.. :|

What I just did instead is show in that Combo Box, list all the items weapons types + arm/helm types and shield types all together.. NOT WHAT I DESIRED.

Now I just wanna do a simple array of strings for the first ComboBox, to show only the types, AND when user choose from those types in ANOTHER ComboBox to show for example: IF Weapons types selected GetDlgItem and show in second Combo the 17
Items for the Weapon types.

I'm so confused when try to accomplish such a thing.. probably I have to take it easy and even write it on a piece of paper how do I have to do small pice of code and done write.

That's happen when I try to do this in one day, obvious without a great experience of it..

However thank you jonnin you helped me a lot in understand where I did wrong and how to do it more elegant not old style.. I think I should probably learn a bit of C++ too, cause Really C is great but so much HARD code to write..

Until next time Cheers.. and Thank you..!!! :)
Last edited on
its been a while. what I remember of a combo-box is from early visual studio and you could actually hard-code the items in the control, rather than load it up dynamically. Your list is constant, and should be done this way. Then the user picks one and you have an event handler that processes their choice. Should be super easy this way, if it still works like that.
Yup.. I was learning right after C , windows programing and actually I love it, but still if one doesn't have enough experience with C or C++ you can't program in windows, so I started to do this in same time.. and in some ways is a bit confusing, but it's fun and enough hard because one has to think about how to manage all the cases, cause in the end the final product depends on all events handlers and here starts all the had shakes, To create static, edit, listbox, buttons, listview..etc is not a big deal actually is the same function of CreateWindow() and the first argument change for what do you need to... but how to manage all in the main loop when BTN_CLICKED or CBN_SLCHANGE, etc... is a big deal and there you start to see if you learn something about C or C++ cause all you see in windows programing besides windows macros and definitions is C operations or C++ , and if someone prefer working with Union, Python, Java, etc.. I rather prefer C or maybe C++ cause it is more flexible and you can access memory directly then other language do.
Topic archived. No new replies allowed.