0

I have this class with templated functions within it.

Header file

class Entity {
public:
    Entity();

    template<typename T>
    void addComponent();
};

Source file

Entity::Entity() {}

template<typename T>
void Entity::addComponent() {}

Main file

int main()
{
    auto e = Entity();
    e.addComponent<Transform>();
}

When compiling it returns this error and I have not idea why. People mention the declared function and the one implemented are not the same but I am not sure if that is the case for this.

main.cpp:(.text+0x1c): undefined reference to `void Entity::addComponent<Transform>()'
/usr/bin/ld: main.cpp:(.text+0x28): undefined reference to `void Entity::addComponent<Sprite>()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:21: build] Error 1
  • 3
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Algirdas Preidžius Sep 27 '21 at 13:33

0 Answers0