Search code examples
c++compiler-errorssfmlunresolved-external

C++ SFML Gamedev Book - Unresolved External Symbol from ResourceHolder class


I have the following three files, of which I cannot find the source of an error that it is producing:

Main.cpp

#include <SFML/Graphics.hpp>
#include <iostream>

#include "ResourceHolder.h"

namespace Textures
{
    enum ID { Landscape, Airplane, Missile };
}

int main()
{
    //...

    try
    {
        ResourceHolder<sf::Texture, Textures::ID> textures;
        textures.load(Textures::Airplane, "Airplane.png");
    }
    catch (std::runtime_error& e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
    }

    //...
}

ResourceHolder.h

#pragma once

#include <map>
#include <string>
#include <memory>
#include <stdexcept>
#include <cassert>

template <typename Resource, typename Identifier>
class ResourceHolder
{
public:
    void load(Identifier id, const std::string& fileName);

    Resource& get(Identifier id);
    const Resource& get(Identifier id) const;

private:
    void insertResource(Identifier id, std::unique_ptr<Resource> resource);

    std::map<Identifier, std::unique_ptr<Resource>> mResourceMap;
};

ResourceHolder.cpp

#include "ResourceHolder.h"

template <typename Resource, typename Identifier>
void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& fileName)
{
    //Create and load resource
    std::unique_ptr<Resource> resource(new Resource());
    if (!resource->loadFromFile(fileName)) {
        throw std::runtime_error("ResourceHolder::load - Failed to load " + fileName);
    }

    //If loading was successful, insert resource to map
    insertResource(id, std::move(resource));
}

template <typename Resource, typename Identifier>
Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
{
    auto found = mResourcemap.find(id);
    assert(found != mResourceMap.end());

    return *found->second();
}

template <typename Resource, typename Identifier>
void ResourceHolder<Resource, Identifier>::insertResource(Identifier id, std::unique_ptr<Resource> resource)
{
    //Insert and check success
    auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource)));
    assert(inserted.second);
}

If I were to remove the try-catch combination in main.cpp, the code compiles fine; However, if I leave it there it gives me an LNK2019 (Unresolved external symbol) Error.

What is the source of this error, and how would I fix it?


Solution

  • You can't define templates inside .cpp files. They have to be defined in the header so the compiler can see the implementation and generate the specific classes.

    Here's a better question/answer on why it is so Why can templates only be implemented in the header file?.

    EDIT: What's wrong in the get function

    Two things.

    First is this auto found = mResourcemap.find(id);. Your map name is incorrect, m should be upper case -> mResourceMap.

    Then the line return *found->second();. The map iterator contains a pair, and the first and second members are not functions but data members. You should write return *found->second;.

    I would advise you to understand the structures you're working with before using templates. The compile errors with templates are pretty messy and harder to read. Also you could make a separate test program and make a resource manager with no templates to understand your errors more easily, then build the template on top of your working resource manager.