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(i).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
    String m_Name = "";
    Sprite m_Sprite;
    Text m_QuantityText;
    Font m_Font;
    int m_Quantity = 0;
    int m_StacksTo = 1;
    int m_Value = 0;

    int m_Hudsize;
    int m_Padding;
    Item(String name, String spriteFileName, int quantity, int stacksTo, int value, Font primaryFont, int hudSize, int padding);
    String name();
    Sprite sprite();
    Vector2f position();
    Text text();
    int quantity();
    void add(int amount);
    void setFont(Font font);
    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, int hudSize, int padding)
    m_Name = name;
    m_Sprite.setOrigin(32, 32);
    m_Quantity = quantity;
    m_StacksTo = stacksTo;
    m_Value = value;
    m_Font = primaryFont;
    std::stringstream string;
    string << m_Quantity;
    m_QuantityText.setOrigin(m_QuantityText.getGlobalBounds().width, 0);
    m_QuantityText.setPosition(0, 0);

    m_Hudsize = hudSize;
    m_Padding = padding;

The item's initialisation Item Sword = Item("Sword", "graphics/Sword.png", 1, 4, 20, primaryFont, hudSize, padding);

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

Item Player::lookAtItem(int position)
    return m_Hotbar[position];

m_Hotbar is an array of Items


So I now have the font of each Item stored as a member variable and I also changed a few other things in the code outside what is here and in what is here. I'm not entirely sure what fixed it.

asked on Stack Overflow Feb 16, 2021 by Ty Doubikin • edited Feb 18, 2021 by Ty Doubikin

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0