jeudi 5 juillet 2018

C++ undefined reference error to existing function [duplicate]

This question already has an answer here:

the last time I did much C++ was back in undergrad nearly 8 years ago, so I have forgotten almost all of it with the paradigms being replaced with C, Fortran Java and Python, so I apologize for my ignorance on the subject. I am currently working through SFML tutorials and attempting to implement a resource loader. I have:

ResourceHolder.hpp:

#ifndef BOOK_RESOURCEHOLDER_HPP
#define BOOK_RESOURCEHOLDER_HPP

#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);
    private:
        void            insertResource(Identifier id, std::unique_ptr<Resource> resource);

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

};

#endif // BOOK_RESOURCEHOLDER_HPP

ResourceHolder.cpp:

#include <ResourceHolder.hpp>

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 successful, insert resource to map
    insertResource(id, std::move(resource));
}

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);
}

TexturesID.hpp:

#ifndef TEXTURESID_HPP
#define TESTURESID_HPP


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

#endif // TEXTURESID_HPP

main.cpp:

int main()
{    
    // Try to load resources
    ResourceHolder<sf::Texture, Textures::ID> textures;
    try
    {
        textures.load(Textures::Landscape, "Media/Textures/Desert.png");
        textures.load(Textures::Airplane, "Media/Textures/Eagle.png");
    }
    catch (std::runtime_error& e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
        return 1;
    }
}

When I attempt to compile this (c++ 11 standard, gcc), I get:

||=== Build: Debug in sfml_tutorial_02 (compiler: GNU GCC Compiler) ===| obj\Debug\source\core\main.o||In function main':| C:\Users\Graham\CodeBlocks\sfml_tutorial_02\source\core\main.cpp|26|undefined reference toResourceHolder::load(Text::ID, std::__cxx11::basic_string, std::allocator > const&)'| C:\Users\Graham\CodeBlocks\sfml_tutorial_02\source\core\main.cpp|27|undefined reference to ResourceHolder<sf::Texture, Text::ID>::load(Text::ID, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'| C:\Users\Graham\CodeBlocks\sfml_tutorial_02\source\core\main.cpp|36|undefined reference toResourceHolder::get(Text::ID)'| C:\Users\Graham\CodeBlocks\sfml_tutorial_02\source\core\main.cpp|37|undefined reference to `ResourceHolder::get(Text::ID)'| ||error: ld returned 1 exit status| ||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Any help figuring out why this is an undefined reference would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire