#LNK2019 Error templated component system

9 messages · Page 1 of 1 (latest)

split token
#

Trying to make a component system similar to Unity's component system. I am facing a LNK2019 error whenever I call the following function:

template<typename T>
std::weak_ptr<T> aze::GameObject::AddComponent()
{
    std::is_base_of<Component, T> isBased{};
    assert(isBased.value && "Not a component.");

    auto pT = std::make_shared<T>();
    auto pComponent = std::weak_ptr<T>(pT);
    m_pComponents.push_back(std::move(pT));

    return pComponent;
}

Since LNK2019 errors are often really broad it is a pain in the ass to find them. Now, I feel like I can't find it because it is a templated function and I am not familiar with solving templated errors.

Anywhere to look on what the cause of the issue could be?

#
void load()
{
    auto& scene = aze::SceneManager::GetInstance().CreateScene("Demo");

    auto go = std::make_shared<aze::GameObject>();
    go->SetTexture("background.tga");
    scene.Add(go);

    go = std::make_shared<aze::GameObject>();
    go->SetTexture("logo.tga");
    go->SetPosition(216, 180);
    scene.Add(go);

    auto font = aze::ResourceManager::GetInstance().LoadFont("Lingua.otf", 36);
    auto to = std::make_shared<aze::GameObject>();
    auto toc = to->AddComponent<aze::TextObject>();
    /*toc->SetFont(font);
    toc->SetText("Programming 4 Assignment");
    toc->SetPosition(80, 20);
    scene.Add(to);*/
}

int main(int, char*[]) {
    aze::Azeban engine("../Data/");
    engine.Run(load);
    return 0;
}
#

This is the piece of code where the error is found, on the line where it says auto toc = to->AddComponent<aze::TextObject>();

#

TextObject is inherited from Component if that matters

#

Exact error is as follows:

1>Main.obj : error LNK2019: unresolved external symbol "public: class std::weak_ptr<class aze::TextObject> __cdecl aze::GameObject::AddComponent<class aze::TextObject>(void)" (??$AddComponent@VTextObject@aze@@@GameObject@aze@@QEAA?AV?$weak_ptr@VTextObject@aze@@@std@@XZ) referenced in function "void __cdecl load(void)" (?load@@YAXXZ)
#

Seems like all my templated functions inside GameObject have linking errors

#

It seems the issue is that templated functions can't be in .cpp files

#

Moving the functions in the header files got rid of the linking errors

#

!solved