I have a problem with templates in c++, I have a method that uses them and I declare it in its file .hpp
but within the logic of the function I still declare the template but it sends me an error.
// entity.hpp
class Entity {
public:
// Start a empty Entity (without transform component)
Entity(unsigned int id);
// Manage the component entry and exist of the entity
void removeComponent ();
template<typename ComponentType, typename ... ComponentArguments>
void addComponent (ComponentArguments&& ... componentArguments );
// Call the base component functions each frame
void start ();
void update();
private:
// List of component of the Entity
std::vector<Component*> components;
};
// entity.cpp
template<typename ComponentType, typename ... ComponentArguments>
void Entity::addComponent (ComponentArguments&& ... componentArguments) {
// Create the new component without specific type passing his arguments (fancy c++ code) and append his owner
ComponentType *newComponent(new ComponentType(std::forward<ComponentArguments>(componentArguments)...));
// Add the new component to the component list and mask
components.emplace_back(newComponent);
}
The error is this, the truth is that I do not understand very well what is happening
PS C:\Users\josem\Documents\game_engine> make
g++ ./src/main.cpp ./src/ECS/entity.cpp -I ./includes -L ./lib -Wall -lraylib -lopengl32 -lgdi32 -lwinmm -o game.exe -O2
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\josem\AppData\Local\Temp\cckj8wGD.o:main.cpp:(.text.startup+0x1e): undefined reference to `void Entity::addComponent<Position>()'
collect2.exe: error: ld returned 1 exit status
make: *** [all] Error 1
I don't know what I have to change to make it work, when I have everything in the file .hpp
it works, but I would like to have everything in 2 files (not sure if that is good practice though, if not please let me know)