Initialization list for array of struct

Hello,
I would like to creat an array of struct objects all created by initialization list. Here is what I have:

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
44
45
46
47
48
49
  //My struct ( I know its not in English but I think names doesnt really matter
struct Umiejetnosci
{
	int ID;
	string Nazwa;
	string Opis;
	string Efekt;
	int IloscAtakow;
	int Obrazenia;
	char TypObrazen;
	int RedukcjaObrazen;
	int Inicjatywa;
	int PunktyWytrzymalosci;
	char Cel;
	char Typ;
	char Dzialanie;
	char Castowanie;
	string Status;
	int Znaczniki;
	string NiwelacjaZnacznikow;
	int Koszt;
	char Wymagania;
	int MaksymalnyPoziom;
};

//Then I got my where I create the array and want to put some values to it function:
(...)
Umiejetnosci TablicaUmiejetnosci[50];
UstanawianieUmiejetnosci(TablicaUmiejetnosci);
(...)

//My Function Prototype:
void UstanawianieUmiejetnosci( Umiejetnosci TablicaUmiejetnosci[] );

//And finally that function with Initialization lists
void UstanawianieUmiejetnosci( Umiejetnosci TablicaUmiejetnosci[] )
{
	TablicaUmiejetnosci[0] = { 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 1, 2, "m", 3, 4, 5, "p", "a", "s", "c", "Podpalony", 6, "Niwelacja", 7, "d", 5 };
        TablicaUmiejetnosci[1] = (...)
        TablicaUmiejetnosci[2] = (...)
(...)
        TablicaUmiejetnosci[49] = (...)
}
//The errors are:
//1>GierkaRPG.cpp(593): error C2059: syntax error : '{'
//1>GierkaRPG.cpp(593): error C2143: syntax error : missing ';' before '{'
//1>GierkaRPG.cpp(593): error C2143: syntax error : missing ';' before '}'
//593 is a line with:
// " TablicaUmiejetnosci[0] = { 0, "Najwidoczniej dziala", "Jakis opis", "Jakis //efekt", 1, 2, "m", 3, 4, 5, "p", "a", "s", "c", "Podpalony", 6, "Niwelacja", //7, "d", 5 }; " 

Can someone please tell me how initialization list should look like? :( I would be very thankful for help.
Last edited on
Ive changed what you said. Still the same errors :( I can only do it like this:
1
2
3
4
TablicaUmiejetnosci[0].ID = 0; //int
TablicaUmiejetnosci[0].Nazwa = "Something";
TablicaUmiejetnosci[0].Opis = "Something2";
(...)

But I would really like to make it with initialization list.
Last edited on
It might look exaclty as you did, except lookout for quotes: " vs ' . Single for char and double for string
 
TablicaUmiejetnosci[0] ={ 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 1, 2, 'm', 3, 4, 5, 'p', 'a', 's', 'c', "Podpalony", 6, "Niwelacja", 7, 'd', 5 };
I have no idea why my reply is above yours :P
Magic!
Furjoza
Still the same errors
Well, error is about missing ; . Didn't you put comments in the middle of the line somewhere? The code works for me.
1
2
3
4
5
6
7
void UstanawianieUmiejetnosci(Umiejetnosci TablicaUmiejetnosci[]){
	TablicaUmiejetnosci[0] = { 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 1, 2, 'm', 3, 4, 5, 'p', 'a', 's', 'c', "Podpalony", 6, "Niwelacja", 7, 'd', 5 };
}
int main(){
	Umiejetnosci u[50];
	UstanawianieUmiejetnosci(u);
}
Last edited on
Its not working ;( The problems with comments is only in my post. Its okey in my program. Is it possible thats fault of my compiler? Im using Visual 2010 Express (Yeah I know its not the best one but I got used to it). When Im writing something like:
1
2
3
4
void UstanawianieUmiejetnosci( Umiejetnosci TablicaUmiejetnosci[] )
{
Umiejetnosci TablicaUmiejetnosci[0] = { 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 1, 2, 'm', 3, 4, 5, 'p', 'a', 's', 'c', "Podpalony", 6, "Niwelacja", 7, 'd', 5 };
}

1>GierkaRPG.cpp(593): error C2466: cannot allocate an array of constant size 0
1>GierkaRPG.cpp(593): error C2082: redefinition of formal parameter 'TablicaUmiejetnosci'
Is it possible that my compiler is taking that table for something else?

EDIT:
Ive made a new project. Here is what I have there:
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
#include "stdafx.h"
#include <stdio.h>
#include <iostream>// std::cout, std::endl
#include <string>//Jak sama nazwa wskazuje
using namespace std;

struct Umiejetnosci
{
	int ID;
	string Nazwa;
	string Opis;
	string Efekt;
	int IloscAtakow;
	int Obrazenia;
	char TypObrazen;
	int RedukcjaObrazen;
	int Inicjatywa;
	int PunktyWytrzymalosci;
	char Cel;
	char Typ;
	char Dzialanie;
	char Castowanie;
	string Status;
	int Znaczniki;
	string NiwelacjaZnacznikow;
	int Koszt;
	char Wymagania;
	int MaksymalnyPoziom;
};
void UstanawianieUmiejetnosci(Umiejetnosci TablicaUmiejetnosci[])
{
	TablicaUmiejetnosci[0] = { 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 1, 2, 'm', 3, 4, 5, 'p', 'a', 's', 'c', "Podpalony", 6, "Niwelacja", 7, 'd', 5 };
}
int main()
{
	Umiejetnosci u[50];
	UstanawianieUmiejetnosci(u);
    return 0;
}

Still the same problem :(
Last edited on
Just to make it right RIGHT:
- Create a new project in VS
- Select Console program
- In creation wizard click NEXT (not FINISH)
- Disable precompiled headers
- Enable empty project
- Finish
- Right click project name in project explorer, select properties
- Go to C++ / General / Warning Level and set to W4
- Create new CPP file
- Paste the same you did above
- Remove first import #include "stdafx.h"
- Hit Compile
- Paste us the error output.

I might learn something too :)
Last edited on
1>------ Build started: Project: cos2, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>c:\documents and settings\rozjebundo\moje dokumenty\visual studio 2010\Projects\cos2\Debug\cos2.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Problem with "- Go to C++ / General / Warning Level and set to W4"
I cant find "C++" is it possible that Express version doesnt have it?
https://dl.dropboxusercontent.com/u/15254827/problem.JPG
still uploading
http://i57.tinypic.com/2ds06fo.jpg
Last edited on
I did a little research: installed vs2010Express (I have 2013 express), did as I suggested to you, and whooa! It IS a compiler issue!
http://stackoverflow.com/questions/3869963/compound-literals-in-msvc
But, hey, in 2010?

Anyway, there is a compiler independent solution you may consider. Have you read about classes already? You can upgrade the Umiejetnosci struct by providing it a constructor. This changes a little the syntax, but works exactly as well. Just two lines needed to be added (very long ones):

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <iostream>// std::cout, std::endl
#include <string>//Jak sama nazwa wskazuje
using namespace std;

struct Umiejetnosci
{
	int ID;
	string Nazwa;
	string Opis;
	string Efekt;
	int IloscAtakow;
	int Obrazenia;
	char TypObrazen;
	int RedukcjaObrazen;
	int Inicjatywa;
	int PunktyWytrzymalosci;
	char Cel;
	char Typ;
	char Dzialanie;
	char Castowanie;
	string Status;
	int Znaczniki;
	string NiwelacjaZnacznikow;
	int Koszt;
	char Wymagania;
	int MaksymalnyPoziom;

	// constructor - a function with no return type and name identical 
	// to struct/class name. In this case with a very long list of parameters.
	Umiejetnosci ( int id, string nazwa, string opis, string efekt, int iloscAtakow,
		int obrazenia, char typObrazen, int redukcjaObrazen, int inicjatywa, 
		int punktyWytrzymalosci, char cel, char typ, char dzialanie, char castowanie, 
		string status, int znaczniki, string niwelacjaZnacznikow, int koszt, 
		char wymagania, int maksymalnyPoziom)  // no semicolon yet
		:	// Initializer list starts with colon. It links paramaters to struct members
		ID(id), Nazwa(nazwa), Opis(opis), Efekt(efekt), IloscAtakow(iloscAtakow), 
		Obrazenia(obrazenia), TypObrazen(typObrazen), RedukcjaObrazen(redukcjaObrazen), 
		Inicjatywa(inicjatywa), PunktyWytrzymalosci(punktyWytrzymalosci), Cel(cel), 
		Typ(typ), Dzialanie(dzialanie), Castowanie(castowanie), Status(status), 
		Znaczniki(znaczniki), NiwelacjaZnacznikow(niwelacjaZnacznikow), Koszt(Koszt), 
		Wymagania(Wymagania), MaksymalnyPoziom(MaksymalnyPoziom)
		{ }; // Empty body of a function - not needed. And semicolon finally

	// no argument contructor, so we can still create empty structs. Also no body;
	Umiejetnosci() {}; 
		
};
void UstanawianieUmiejetnosci(Umiejetnosci TablicaUmiejetnosci[])
{
	// create by calling constructor with all required parameters just like any other function:
	TablicaUmiejetnosci[0] = Umiejetnosci( 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 
		1, 2, 'm', 3, 4, 5, 'p', 'a', 's', 'c', "Podpalony", 6, "Niwelacja", 7, 'd', 5 );
}
int main()
{
	Umiejetnosci u[50];
	UstanawianieUmiejetnosci(u);
	return 0;
}


EDIT: I know such projects tend to not get completed, but please, share the code somewhere like code.google.com, bitbucket.com or github. I am curious what comes out of this super complex RPG :)
Last edited on
Wow dude. You have put a lot of afford in helping me. Im really thankful. I didnt know there is constructor in structures. Actully its not working in that second project. Still the same problem with that LNK but when I copied your code to my first project it is somehow working. Can you tell me something more about what actully happened? Its like my compiler didnt know it is a table so we needed to add constructor and write it "
1
2
TablicaUmiejetnosci[0] = Umiejetnosci( 0, "Najwidoczniej dziala", "Jakis opis", "Jakis efekt", 
		1, 2, 'm', 3, 4, 5, 'p', 'a', 's', 'c', "Podpalony", 6, "Niwelacja", 7, 'd', 5 );
" in unambiguous way?
Last edited on
The feature you compiler is not supporting is called "compound literals" and is part of C99 standard. It evidently was included only in VS2013: http://msdn.microsoft.com/en-us/library/hh409293.aspx . (If you are on Windows 7 or 8, an update would be possible).

Compiler indeed could not see an object of type Umiejetnosci between curly braces, so it could not assign it to a variable.
Constructor on the other hand is one of the core features of C++, so it must be working even on very old implementations (not ANSI C compilers though! C++ only).
Gratitude. Thanks again for your afford ;)
Topic archived. No new replies allowed.