0

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?

  • The error is this: error LNK2019: unresolved external symbol "public: void __thiscall ResourceHolder::load(enum Textures::ID,class std::basic_string,class std::allocator > const &)" (?load@?$ResourceHolder@VTexture@sf@@W4ID@Textures@@@@QAEXW4ID@Textures@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main –  Jun 28 '15 at 09:24

2 Answers2

0

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.

Community
  • 1
  • 1
aslg
  • 1,966
  • 2
  • 15
  • 20
  • That's interesting. I put the code from the CPP into the H file and now the code works fine, however I can't use the get function to set textures for sprites, etc. Do you know what could be causing the compile error when using something like "textures.get(Textures::Airplane)"? –  Jun 28 '15 at 11:17
  • Thanks. I changed everything like you said and now it works perfectly. –  Jun 28 '15 at 13:26
0

With all the other answers providing you with enough information to why your code don't compile and might not be valid, this is a resource manager i wrote for SFML some time ago, might be of use to you:

HPP FILE:

#ifndef RESOURCEMANAGER_HPP
#define RESOURCEMANAGER_HPP

/************ INCLUDES ***********/
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <memory>
#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"


class ResourceManager
{
private:
    std::map<std::string,std::unique_ptr<sf::Texture>> listImageContainer;
    std::map<std::string,std::pair<std::unique_ptr<sf::SoundBuffer>,std::unique_ptr<sf::Sound>>> listSoundContainer;
    std::map<std::string,std::unique_ptr<sf::Font>> listFontContainer;

public:
    ResourceManager();
    std::unique_ptr<sf::Sound>& LoadSound(const std::string);
    std::unique_ptr<sf::Font>& LoadFont(const std::string);
    std::unique_ptr<sf::Texture>& LoadImage(const std::string);
    ~ResourceManager();
};
#endif

CPP FILE:

#include "ResourceManager.hpp"

ResourceManager::ResourceManager()
{

}

std::unique_ptr<sf::Sound>& ResourceManager::LoadSound(const std::string _fileName)
{
    if (listSoundContainer.find(_fileName) == listSoundContainer.end())
    {
        std::unique_ptr<sf::SoundBuffer> soundBuffer(new sf::SoundBuffer());
        if (soundBuffer->loadFromFile("assets/sound/" + _fileName) != false)
        {
            std::unique_ptr<sf::Sound> sound(new sf::Sound(*soundBuffer));
            listSoundContainer[_fileName] = std::make_pair(std::move(soundBuffer), std::move(sound));
            return listSoundContainer[_fileName].second;
        }
        else
        {
            std::cerr << "Error loading sound..." << std::endl;
        }
    }
    else
    {
        return listSoundContainer[_fileName].second;
    }
}

std::unique_ptr<sf::Font>& ResourceManager::LoadFont(const std::string _fileName)
{
    if (listFontContainer.find(_fileName) == listFontContainer.end())
    {
        std::unique_ptr<sf::Font> font(new sf::Font());
        if (font->loadFromFile("assets/font/" + _fileName)!=false)
        {
            listFontContainer[_fileName] = std::move(font);
            return listFontContainer[_fileName];
        }
        else
        {
            std::cerr << "Error loading font..." << std::endl;
        }
    }
    else
    {
        return listFontContainer[_fileName];
    }
}

std::unique_ptr<sf::Texture>& ResourceManager::LoadImage(const std::string _fileName)
{
    if (listImageContainer.find(_fileName) == listImageContainer.end())
    {
        std::unique_ptr<sf::Texture> texture(new sf::Texture);
        if (texture->loadFromFile("assets/image/" + _fileName)!=false)
        {
            listImageContainer[_fileName] = std::move(texture);
            return listImageContainer[_fileName];
        }
        else
        {
            std::cerr << "Error loading image: " << _fileName << std::endl;
        }
    }
    else
    {
        return listImageContainer[_fileName];
    }
}

ResourceManager::~ResourceManager(){}

How to use:

ResourceManager resourceManager;
auto& sound = resourceManager.LoadSound("nice.wav");
auto& image = resourceManager.LoadImage("head.png");
auto& sound2 = resourceManager.LoadSound("nice.wav"); //<--- already loaded
sound.play();
etc...