samedi 16 juillet 2022

multiple definition, but it is the same definition [closed]

I have a problem that I can't solve, I'm making a little game for the university. In view of the review with the professor, I thought about refactoring the program. The problem is that I currently have a TurnSystem class that manages the turns and movement of the pieces. To make it work I use a file taken from the internet (https://www.redblobgames.com/pathfinding/a-star/implementation.html) reworked and adapted for my program. I have split this file into two Map.cpp and AStar.cpp. In the program right now, every time I need the map I have to recreate it (instead of modifying it), which greatly increases the computation cost.

The problem is that as long as the code is TurnSystem.cpp

includes "TurnSystem.h"
includes "AStar.cpp"
void TurnSystem::method{
   SquareGrid grid = MakeDiagram();
   //code
}

the code works. But if I change it like TurnSystem.h

includes "Librery.h"
includes "AStar.cpp"
class TurnSystem{
private:
   SquareGrid grid;
   //code
}

the code gives me error, saying that I am declaring different parts of Astar.cpp and Map.cpp several times es:

C: \ Program Files \ JetBrains \ CLion 2021.3.3 \ bin \ mingw \ bin / ld.exe: CMakeFiles / Reisende.dir / Game.cpp.obj: in function `__gnu_cxx :: new_allocator <std :: __ detail :: _ Hash_node_base *> :: new_allocator () ':
C: /Users/franc/Desktop/Reisende/Map.cpp: 74: multiple definition of `operator == (GridLocation, GridLocation) '; CMakeFiles / Reisende.dir / main.cpp.obj: C: /Users/franc/Desktop/Reisende/Map.cpp: 74: first defined here

TurnSystem is in turn included in a Game class, which in turn is included in Main.

I don't know how to fix it. I also thought about making grid a global variable. But I know it's not good practice Ideas? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire