现有函数的C ++未定义引用错误

时间:2018-07-05 18:33:19

标签: c++ c++11

我上一次做C ++的最后一次是在将近8年前回到本科,所以我几乎忘记了所有这一切,因为范式已被C,Fortran Java和Python取代,因此我对这个主题的无知表示歉意。我目前正在研究SFML教程,并尝试实现资源加载器。我有:

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

当我尝试编译此代码(c ++ 11标准,gcc)时,我得到:

|| ===构建:在sfml_tutorial_02中进行调试(编译器:GNU GCC编译器)=== | obj \ Debug \ source \ core \ main.o ||在函数main':| C:\Users\Graham\CodeBlocks\sfml_tutorial_02\source\core\main.cpp|26|undefined reference to中ResourceHolder :: load(Text :: ID,std :: __ cxx11 :: basic_string,std :: allocator> const&)'| C:\ Users \ Graham \ CodeBlocks \ sfml_tutorial_02 \ source \ core \ main.cpp | 27 |对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 to ResourceHolder :: get(Text :: ID)'|的未定义引用 C:\ Users \ Graham \ CodeBlocks \ sfml_tutorial_02 \ source \ core \ main.cpp | 37 |未定义对`ResourceHolder :: get(Text :: ID)'的引用| ||错误:ld返回1个退出状态| || ===构建失败:5个错误,0个警告(0分钟,0秒)=== |

任何帮助弄清为什么这是一个未定义的引用,将不胜感激。

0 个答案:

没有答案