Возникло исключение: нарушение прав доступа для чтения. _Страшно было nullptr - PullRequest
0 голосов
/ 25 мая 2020

Попытка научиться программировать с помощью Джима Хортона «Начало программирования на C ++ с Джимом Хортоном». Вылетает следующее:

ZombieArena.exe!std::_Tree<std::_Tmap_traits<std::string,sf::Texture,std::less<std::string>,std::allocator<std::pair<std::string const ,sf::Texture>>,0>>::_Find_lower_bound<std::string>(const std::string & _Keyval) Line 1704    C++
ZombieArena.exe!std::_Tree<std::_Tmap_traits<std::string,sf::Texture,std::less<std::string>,std::allocator<std::pair<std::string const ,sf::Texture>>,0>>::_Find<std::string>(const std::string & _Keyval) Line 1457    C++
ZombieArena.exe!std::_Tree<std::_Tmap_traits<std::string,sf::Texture,std::less<std::string>,std::allocator<std::pair<std::string const ,sf::Texture>>,0>>::find(const std::string & _Keyval) Line 1466  C++
ZombieArena.exe!TextureHolder::GetTexture(const std::string & filename) Line 21 C++
ZombieArena.exe!Zombie::spawn(float startX, float startY, int type, int seed) Line 28   C++

Код для этого:

    #pragma once

#ifndef TEXTURE_HOLDER_H
#define TEXTURE_HOLDER_H
#include <SFML/Graphics.hpp>
#include <map>

using namespace sf;
using namespace std;

class TextureHolder
{
private:
    // A map container from the STL,
    // that holds related pairs of String and Texture
    map< string, Texture> m_Textures;
    // A pointer of the same type as the class itself
    // the one and only instance
    static TextureHolder* m_s_Instance;
public:
    TextureHolder();
    static Texture& GetTexture(string const& filename);
};
#endif

Файл cpp:

    #include "TextureHolder.h"

// Include the "assert feature"
#include <assert.h>

TextureHolder* TextureHolder::m_s_Instance = nullptr;
TextureHolder::TextureHolder()
{
    assert(m_s_Instance == nullptr);
    m_s_Instance = this;
}

Texture& TextureHolder::GetTexture(string const& filename)
{
    // Get a reference to m_Textures using m_s_Instance
    auto& m = m_s_Instance->m_Textures;
    // auto is the equivalent of map<string, Texture>
    // Create an iterator to hold a key-value-pair (kvp)
    // and search for the required kvp
    // using the passed in file name
    auto keyValuePair = m.find(filename);
    // auto is equivalent of map<string, Texture>::iterator
    // Did we find a match?
    if (keyValuePair != m.end())
    {
        // Yes
        // Return the texture,
        // the second part of the kvp, the texture
        return keyValuePair->second;
    }
    else
    {
        // File name not found
        // Create a new key value pair using the filename
        auto& texture = m[filename];
        // Load the texture from file in the usual way
        texture.loadFromFile(filename);
        // Return the texture to the calling code
        return texture;
    }
}

Он падает на 2-я автоматическая линия для поиска карты. Любые идеи???? Я запутался. Заранее спасибо.

...