Bonjour, je suis actuellement sur un projet c++ et je rencontre quelques difficultés avec la compilation. En clair, je ne vois pas très clairement comment je dois organiser mon code (modularité), et je crois que c'est la cause de certaines de mes erreurs de compilations.
------
Pour entrer dans le détail, voici l'architecture de mon programme:
Test.cpp...................................................................(Contient la fonction main)
.....L.. Partie.cpp.......................................................(Classe)
...........L.. G_Init.h ...................................................(implémentation des méthodes d'initialisation d'une partie)
...........I........... L.. G_Init_tools.h .............................(fonctions annexes à la classe Partie qui servent à initialiser une partie)
...........L.. G_Loop.h..................................................(implémentation de la méthode qui gère le tour de jeu)
...........L.. G_Render.h...............................................( " méthode d'affichage)
...........I...........L.. G_Render_tools.h...........................(fonctions annexes pour l'affichage)
...........L.. G_Terminate.h ...........................................(méthode de libération de la mémoire)
* Les "L.. " correspondent à mes includes.
------
Tout d'abord, cette facon d'organiser mon programme est-elle correcte? Sinon, comment dois-je procéder?
Ensuite, j'utilises Visual Studio pour mon projet et j'ai des erreurs de linker (LNK2005 (fonctions/méthodes déja définie) sur mes fonctions/méthodes -comprendre celles que j'ai écrites-).
Etant donné que ces erreurs se produisent sur les méthodes que j'ai personnellement déclaré, il me semble qu'il ne s'agit pas d'un problème de configuration des Bibliothèques Runtime (j'ai déja vérifié,
le mode debug et release sont configurés sur /MD -Multithread-).
Si je comprends bien les erreurs qu'il me donne, les fonctions sont compilées dans deux fichiers objets (test.o et partie.o), ce qui pose problème lors de la création de l'executable (les fonctions/methodes se
trouvent définies dans plusieurs fichiers).
Comment puis-je régler ca?
------
INFOS SUR LE CODE
test.cpp:
Partie.cpp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 #ifndef _test_ #define _test_ #include "Partie.cpp" int main(int argc, char ** argv){ Partie p(""); exit(EXIT_SUCCESS); } #endif
G_Render:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74 #ifndef _Partie_ #define _Partie_ // Librairies standards #include <stdio.h> #include <stdlib.h> #include <iostream> #include <vector> // Librairie SDL #include <SDL2\SDL.h> #include <SDL2\SDL_image.h> // FICHIERS DU PROJETS #include "Partie.h" using namespace std; class Partie{ protected: SDL_Window * window; SDL_Renderer * renderer; SDL_Event eventBuffer; string m_config; // Fichier de config // Etat de la partie bool finish; // Parametres de la partie int terrain[TERRAIN_W][TERRAIN_H]; // Matrice du terrain vector<SDL_Texture *> tiles; public: // DESCRIPTION D'UNE PARTIE Partie(string config) : window(NULL), renderer(NULL), m_config(config), finish(false), tiles() { // Initialisation de la fenetre g_init_video(); // Initialisation de la partie g_init_partie(); // boucle de jeu while (!finish){ // Tour de jeu g_loop(); // Affichage du plateau g_render(); } } // LIBERATION DE LA MEMOIRE ~Partie(){ terminate(); } void g_init_video(); void g_init_partie(); void g_loop(); void g_render(); void g_terminate(); }; #include "g_init.h" #include "g_loop.h" #include "g_render.h" #include "g_terminate.h" #endif
G_Render_tools
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #ifndef _g_render_ #define _g_render_ #include "G_Render_TOOLS.h" void Partie::g_render(){ SDL_RenderClear(renderer); // BOUCLE D'AFFICHAGE drawMap(tiles, renderer); SDL_RenderPresent(renderer); } #endif
INFOS SUR LES ERREURS
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef _g_render_tools_ #define _g_render_tools_ /** * Dessine la map */ void drawMap(const vector<SDL_Texture *> & tiles, SDL_Renderer * renderer){ // Terrain int terrain[WINDOW_W][WINDOW_H] = { { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, }; // Pour chaque case du terrain for (int i = 0; i < WINDOW_W; i++){ for (int j = 0; j < WINDOW_H; j++){ // On dessine la tuile associée if (terrain[i][j] != -1){ SDL_Rect dstRect = { i - j, // posX (i + j) / 2, // posY TILE_W, TILE_H }; SDL_RenderCopy(renderer, tiles[terrain[i][j]], NULL, &dstRect); } } } } #endif
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 Erreur 1 error LNK2005: "void __cdecl LoadTilesTerrain(class std::vector<struct SDL_Texture *,class std::allocator<struct SDL_Texture *> > &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,struct SDL_Renderer *)" (?LoadTilesTerrain@@YAXAAV?$vector@PAUSDL_Texture@@V?$allocator@PAUSDL_Texture@@@std@@@std@@AAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@2@PAUSDL_Renderer@@@Z) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 2 error LNK2005: "void __cdecl drawMap(class std::vector<struct SDL_Texture *,class std::allocator<struct SDL_Texture *> > const &,struct SDL_Renderer *)" (?drawMap@@YAXABV?$vector@PAUSDL_Texture@@V?$allocator@PAUSDL_Texture@@@std@@@std@@PAUSDL_Renderer@@@Z) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 3 error LNK2005: "public: void __thiscall Partie::g_init_partie(void)" (?g_init_partie@Partie@@QAEXXZ) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 4 error LNK2005: "public: void __thiscall Partie::g_init_video(void)" (?g_init_video@Partie@@QAEXXZ) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 5 error LNK2005: "public: void __thiscall Partie::g_loop(void)" (?g_loop@Partie@@QAEXXZ) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 6 error LNK2005: "public: void __thiscall Partie::g_render(void)" (?g_render@Partie@@QAEXXZ) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 7 error LNK2005: "public: void __thiscall Partie::g_terminate(void)" (?g_terminate@Partie@@QAEXXZ) déjà défini(e) dans Partie.obj C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Unity8012\test.obj Unity8012 Erreur 8 error LNK1169: un ou plusieurs symboles définis à différentes reprises ont été rencontrés C:\Users\CreiZyz\Documents\Visual Studio 2013\Projects\Unity8012\Debug\Unity8012.exe Unity8012
Partager