c++: erreur au changement d'une valeur d'un struct
Posté le 21/01/2021 13:27
bonjour, je fais un moteur de jeu(~mini unity 2d) pour calculatrice
mais quand je modifie des objets pour la demo j'ai ça:
fxsdk build-fx
:: Making into build-fx
sh-elf-g++ -c src/objects_data.c -o build-fx/src/objects_data.c.o -mb -ffreestanding -nostdlib -fstrict-volatile-bitfields -Wall -Wextra -Wno-missing-field-initializers -Os -D FX9860G -m3 -I include -MMD -MT build-fx/src/objects_data.c.o -MF build-fx/src/objects_data.c.d -MP
src/objects_data.c:9:1: error: 'objects' does not name a type
9 | objects[1].x = 0;
| ^~~~~~~
src/objects_data.c:10:1: error: 'workspace' does not name a type
10 | workspace.objects[1].y = 40;
| ^~~~~~~~~
src/objects_data.c:11:1: error: 'workspace' does not name a type
11 | workspace.objects[1].size_x = 50;
| ^~~~~~~~~
src/objects_data.c:12:1: error: 'workspace' does not name a type
12 | workspace.objects[1].size_y = 20;
| ^~~~~~~~~
src/objects_data.c:13:1: error: 'workspace' does not name a type
13 | workspace.objects[1].tilemap_capture_x = 0;
| ^~~~~~~~~
src/objects_data.c:14:1: error: 'workspace' does not name a type
14 | workspace.objects[1].tilemap_capture_y = 0;
| ^~~~~~~~~
src/objects_data.c:16:1: error: 'workspace' does not name a type
16 | workspace.objects[0].x = 10;
| ^~~~~~~~~
src/objects_data.c:17:1: error: 'workspace' does not name a type
17 | workspace.objects[0].y = 30;
| ^~~~~~~~~
src/objects_data.c:18:1: error: 'workspace' does not name a type
18 | workspace.objects[0].size_x = 4;
| ^~~~~~~~~
src/objects_data.c:19:1: error: 'workspace' does not name a type
19 | workspace.objects[0].size_y = 8;
| ^~~~~~~~~
src/objects_data.c:20:1: error: 'workspace' does not name a type
20 | workspace.objects[0].tilemap_capture_x = 51;
| ^~~~~~~~~
src/objects_data.c:21:1: error: 'workspace' does not name a type
21 | workspace.objects[0].tilemap_capture_y = 1;
| ^~~~~~~~~
src/objects_data.c:22:1: error: 'workspace' does not name a type
22 | workspace.objects[0].gravity = 1;
| ^~~~~~~~~
make: *** [Makefile:121 : build-fx/src/objects_data.c.o] Erreur 1
code:
struct{
struct object objects[OBJECTS];
}workspace;
workspace.objects[1].x = 0;
workspace.objects[1].y = 40;
workspace.objects[1].size_x = 50;
workspace.objects[1].size_y = 20;
workspace.objects[1].tilemap_capture_x = 0;
workspace.objects[1].tilemap_capture_y = 0;
workspace.objects[0].x = 10;
workspace.objects[0].y = 30;
workspace.objects[0].size_x = 4;
workspace.objects[0].size_y = 8;
workspace.objects[0].tilemap_capture_x = 51;
workspace.objects[0].tilemap_capture_y = 1;
workspace.objects[0].gravity = 1;
j'ai essayé TOUTES les manières de définir un struct
edit: j'ai aussi éssayé de mattre dans un
extern "C"
Citer : Posté le 21/01/2021 13:30 | #
Tu t'y prends mal.
struct { … } workspace; défini un modèle de structure. Ça indique au compilateur « voilà comment ma structure est organisée ». Ça ne construit pas une structure en mémoire.
Pour ce faire, la bonne méthode est de déclarer une nouvelle variables, de type struct workspace, puis de travailler dessus.
struct object objects[OBJECTS];
} workspace;
struct workspace my_workspace;
my_workspace.objects[1].x = 0;
my_workspace.objects[1].y = 40;
Citer : Posté le 21/01/2021 13:34 | #
j'ai déjà essayé, comme dit ,
mais j'ai refais et ça donne
:: Making into build-fx
sh-elf-g++ -c src/engine.c -o build-fx/src/engine.c.o -mb -ffreestanding -nostdlib -fstrict-volatile-bitfields -Wall -Wextra -Wno-missing-field-initializers -Os -D FX9860G -m3 -I include -MMD -MT build-fx/src/engine.c.o -MF build-fx/src/engine.c.d -MP
In file included from src/engine.c:7:
include/objects_data.h:7:21: error: aggregate 'eg_workspace workspace' has incomplete type and cannot be defined
7 | struct eg_workspace workspace;
| ^~~~~~~~~
include/objects_data.h:9:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
9 | workspace.objects[1].x = 0;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:10:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
10 | workspace.objects[1].y = 40;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:11:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
11 | workspace.objects[1].size_x = 50;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:12:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
12 | workspace.objects[1].size_y = 20;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:13:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
13 | workspace.objects[1].tilemap_capture_x = 0;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:14:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
14 | workspace.objects[1].tilemap_capture_y = 0;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:16:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
16 | workspace.objects[0].x = 10;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:17:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
17 | workspace.objects[0].y = 30;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:18:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
18 | workspace.objects[0].size_x = 4;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:19:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
19 | workspace.objects[0].size_y = 8;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:20:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
20 | workspace.objects[0].tilemap_capture_x = 51;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:21:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
21 | workspace.objects[0].tilemap_capture_y = 1;
| ^~~~~~~~~
| eg_workspace
include/objects_data.h:22:1: error: 'workspace' does not name a type; did you mean 'eg_workspace'?
22 | workspace.objects[0].gravity = 1;
| ^~~~~~~~~
| eg_workspace
make: *** [Makefile:121 : build-fx/src/engine.c.o] Erreur 1
Citer : Posté le 21/01/2021 13:36 | #
Kbd2 me signale que j'ai mal défini la structure.
…
};
Citer : Posté le 21/01/2021 13:41 | #
The correct syntax would be
struct object objects [OBJECTS];
} workspace;
This is because unlike C, C++ does not allow anonymous structs meaning the struct must be given a type (struct Workspace type in this case).
Citer : Posté le 21/01/2021 13:41 | #
Et pour être précis, j'ai tendance à préférer la syntaxe utilisant un typedef :
…
} data_t; // …et dont le nom est data_t
// On créé une variable de type data_t
data_t my_struct;
// On l'utilise
my_struct.field = 1;
Edit : KBD a raison, mon code s'applique plutôt en C, pas en C++
Citer : Posté le 21/01/2021 14:26 | #
struct { … } workspace; défini un modèle de structure. Ça indique au compilateur « voilà comment ma structure est organisée ». Ça ne construit pas une structure en mémoire.
Pour préciser, c'est correct en C. Dans cette définition, le type est une structure anonyme (struct { ... }) et la variable est workspace. Ça se fait parfois, mais c'est interdit en C++.
Du reste en C++ une structure est une classe, donc pas besoin de typedef et on préfère une majuscule à un _t.
Citer : Posté le 21/01/2021 17:10 | #
j'ai réussit a faire avancer l'erreur!
struct object objects[OBJECTS];
};
//struct eg_workspace workspace;
//eg_workspace workspace;
workspace::objects[1].x = 0;
workspace::objects[1].y = 40;
workspace::objects[1].size_x = 50;
workspace::objects[1].size_y = 20;
workspace::objects[1].tilemap_capture_x = 0;
workspace::objects[1].tilemap_capture_y = 0;
sh-elf-g++ -c src/engine.c -o build-fx/src/engine.c.o -mb -ffreestanding -nostdlib -fstrict-volatile-bitfields -Wall -Wextra -Wno-missing-field-initializers -Os -D FX9860G -m3 -I include -MMD -MT build-fx/src/engine.c.o -MF build-fx/src/engine.c.d -MP
In file included from src/engine.c:9:
include/objects_data.h:11:12: error: 'objects' in 'struct workspace' does not name a type
11 | workspace::objects[1].x = 0;
| ^~~~~~~
include/objects_data.h:5:19: note: 'workspace::objects' declared here
5 | struct object objects[OBJECTS];
| ^~~~~~~
include/objects_data.h:12:12: error: 'objects' in 'struct workspace' does not name a type
12 | workspace::objects[1].y = 40;
| ^~~~~~~
include/objects_data.h:5:19: note: 'workspace::objects' declared here
5 | struct object objects[OBJECTS];
| ^~~~~~~
include/objects_data.h:13:12: error: 'objects' in 'struct workspace' does not name a type
13 | workspace::objects[1].size_x = 50;
| ^~~~~~~
include/objects_data.h:5:19: note: 'workspace::objects' declared here
5 | struct object objects[OBJECTS];
| ^~~~~~~
include/objects_data.h:14:12: error: 'objects' in 'struct workspace' does not name a type
14 | workspace::objects[1].size_y = 20;
| ^~~~~~~
include/objects_data.h:5:19: note: 'workspace::objects' declared here
5 | struct object objects[OBJECTS];
| ^~~~~~~
include/objects_data.h:15:12: error: 'objects' in 'struct workspace' does not name a type
15 | workspace::objects[1].tilemap_capture_x = 0;
| ^~~~~~~
include/objects_data.h:5:19: note: 'workspace::objects' declared here
5 | struct object objects[OBJECTS];
| ^~~~~~~
include/objects_data.h:16:12: error: 'objects' in 'struct workspace' does not name a type
16 | workspace::objects[1].tilemap_capture_y = 0;
| ^~~~~~~
include/objects_data.h:5:19: note: 'workspace::objects' declared here
5 | struct object objects[OBJECTS];
| ^~~~~~~
Citer : Posté le 21/01/2021 17:30 | #
C'est d'un tutoriel de C++ dont tu as besoin là ! Attrape un bouquin et prends le temps d'étudier le langage, tu ne peux pas y aller à l'aveugle.
struct object objects[OBJECTS];
};
Workspace lephes_wp;
lephes_wp.objects[1].x = 0;
lephes_wp.objects[1].y = 40;
lephes_wp.objects[1].size_x = 50;
lephes_wp.objects[1].size_y = 20;
lephes_wp.objects[1].tilemap_capture_x = 0;
lephes_wp.objects[1].tilemap_capture_y = 0;
Et je parie deux contre un que ce [1] devrait être un [0].
Citer : Posté le 21/01/2021 17:47 | #
j'ai déjà fais ça et ça marche pas (toujours la même erreur)
Citer : Posté le 21/01/2021 19:12 | #
Les struct object auront le même problème.