Unhandled exception at 0x7B2B49F8 (sfml-graphics-d-2.dll) in Prepare.exe: 0xC0000005: Access violation reading location

I’m a bit of a beginner with C++ and SFML. I’ve run into an unhandled exception which I don’t understand how to fix. If the code I’ve included isn’t enough to reveal the situation let me know.

I would think it has to do with not being able to refer to m_QuantityText.


This is the line with the unhandled exception
window.draw(player.lookAtItem(0).text());

This is the function called by the line

Text Item::text()
{
    return m_QuantityText;
}

The Header File

#pragma once

#include <SFML/Graphics.hpp>

using namespace sf;

class Item
{
private:
    String m_Name = "";
    Sprite m_Sprite;
    Text m_QuantityText;
    int m_Quantity = 0;
    int m_StacksTo = 1;
    int m_Value = 0;
public:
    Item(String name, String spriteFileName, int quantity, int stacksTo, int value, Font primaryFont);
    String name();
    Sprite sprite();
    Vector2f position();
    Text text();
    int quantity();
    void add(int amount);
    int stacksTo();
    int value();
    void setPosition(Vector2f position);
    void update();
};

This is the constructor for Item

Item::Item(String name, String spriteFileName, int quantity, int stacksTo, int value, Font primaryFont)
{
    m_Name = name;
    m_Sprite.setTexture(TextureManager::loadTexture(spriteFileName));
    m_Sprite.setOrigin(32, 32);
    m_Quantity = quantity;
    m_StacksTo = stacksTo;
    m_Value = value;
    m_QuantityText.setFont(primaryFont);
    m_QuantityText.setCharacterSize(15);
    m_QuantityText.setFillColor(Color::White);
    std::stringstream s;
    s << m_Quantity;
    m_QuantityText.setString(s.str());
    m_QuantityText.setOrigin(m_QuantityText.getGlobalBounds().width, 0);
    m_QuantityText.setPosition(0, 0);
}

The item’s initialisation
Item Sword = Item("Sword", "graphics/Sword.png", 1, 2, 20, primaryFont);

(player.lookAtItem(0) was set to = Sword)

Source: Windows Questions C++

LEAVE A COMMENT