Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Render Window Attempting to Reference deleted function  (Read 2754 times)

0 Members and 1 Guest are viewing this topic.

Jacob309

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Render Window Attempting to Reference deleted function
« on: March 21, 2020, 04:08:17 am »
So I know from other posts that this error occurs when you try to copy something uncountable. Problem is obviously that when I push back the window it copys it. Theoretically the solution to that is a vector of refrances. When I make it a vector of refrances and obviously push back refrances files xmemory and vector freak out, give >20 errors all saying that pointers to refrances are illegal.  How show should I go about fixing this or what is a different method to use.

If this doesn't work I am trying to make a void that spawns a new window and adds it to the vector. The vector runs in a void and functions like the while loop in just one window. I run the void from the while loop of a main window.

The Struct I've made
typedef struct KeyStruct {
    sf::Image Img;
    sf::Texture Tex;
    sf::Sprite Sprite;
    //there is more but its not necessary
}NewKey;

Step Windows
static vector <sf::RenderWindow> WindowArray;
static vector <NewKey> KeyArray;
static void StepWindows()
{
    sf::Clock clock;
    int i;
    for (i = 0; i > WindowArray.size(); i++)
        MakeTopWindow(WindowArray[i].getSystemHandle());
    WindowArray[i].clear(sf::Color::Transparent);
    WindowArray[i].draw(KeyArray[i].Sprite);
    WindowArray[i].display();
    if (clock.getElapsedTime().asMicroseconds() > 1000)
    {
        WindowArray[i].setPosition(MakeKey::Gravity(WindowArray[i], KeyArray[i]));
    }
}

Draw Key
static void DrawKey(string input)
{
    //Declair Key
    NewKey Key;
    NewKey& KeyRef = Key;
    if (input == "A")
        Key.Img.loadFromFile("Assets/Images/A.png");
    else if (input == "D")
        Key.Img.loadFromFile("Assets/Images/D.png");
    //ect
    Key.Tex.loadFromImage(Key.Img);
    Key.Sprite.setTexture(Key.Tex);

    sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
    sf::RenderWindow& windowRef = window;
    window.setPosition(MakeKey::RandSpawn(Key.Img));
    MakeKey::WindowArray.push_back(windowRef);
    MakeKey::KeyArray.push_back(KeyRef);
}

And My Main
int main()
{
    sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            MakeKey::MakeTopWindow(window.getSystemHandle());
            //Key Presses
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::A)
                    MakeKey::DrawKey("A");
                else if (event.key.code == sf::Keyboard::D)
                    MakeKey::DrawKey("D");
                //ect
            }
            //Close
            if (event.type == sf::Event::Closed)
                window.close();
        }
        MakeKey::MakeTopWindow(window.getSystemHandle());
        MakeKey::StepWindows();
    }
    return EXIT_SUCCESS;
}

Thanks for the help
« Last Edit: March 21, 2020, 05:03:46 am by Jacob309 »

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Render Window Attempting to Reference deleted function
« Reply #1 on: March 21, 2020, 08:19:04 am »
So I know from other posts that this error occurs when you try to copy something uncountable. Problem is obviously that when I push back the window it copys it. Theoretically the solution to that is a vector of refrances. When I make it a vector of refrances and obviously push back refrances files xmemory and vector freak out, give >20 errors all saying that pointers to refrances are illegal.  How show should I go about fixing this or what is a different method to use.

If this doesn't work I am trying to make a void that spawns a new window and adds it to the vector. The vector runs in a void and functions like the while loop in just one window. I run the void from the while loop of a main window.

The Struct I've made
typedef struct KeyStruct {
    sf::Image Img;
    sf::Texture Tex;
    sf::Sprite Sprite;
    //there is more but its not necessary
}NewKey;

Step Windows
static vector <sf::RenderWindow> WindowArray;
static vector <NewKey> KeyArray;
static void StepWindows()
{
    sf::Clock clock;
    int i;
    for (i = 0; i > WindowArray.size(); i++)
        MakeTopWindow(WindowArray[i].getSystemHandle());
    WindowArray[i].clear(sf::Color::Transparent);
    WindowArray[i].draw(KeyArray[i].Sprite);
    WindowArray[i].display();
    if (clock.getElapsedTime().asMicroseconds() > 1000)
    {
        WindowArray[i].setPosition(MakeKey::Gravity(WindowArray[i], KeyArray[i]));
    }
}

Draw Key
static void DrawKey(string input)
{
    //Declair Key
    NewKey Key;
    NewKey& KeyRef = Key;
    if (input == "A")
        Key.Img.loadFromFile("Assets/Images/A.png");
    else if (input == "D")
        Key.Img.loadFromFile("Assets/Images/D.png");
    //ect
    Key.Tex.loadFromImage(Key.Img);
    Key.Sprite.setTexture(Key.Tex);

    sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
    sf::RenderWindow& windowRef = window;
    window.setPosition(MakeKey::RandSpawn(Key.Img));
    MakeKey::WindowArray.push_back(windowRef);
    MakeKey::KeyArray.push_back(KeyRef);
}

And My Main
int main()
{
    sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            MakeKey::MakeTopWindow(window.getSystemHandle());
            //Key Presses
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::A)
                    MakeKey::DrawKey("A");
                else if (event.key.code == sf::Keyboard::D)
                    MakeKey::DrawKey("D");
                //ect
            }
            //Close
            if (event.type == sf::Event::Closed)
                window.close();
        }
        MakeKey::MakeTopWindow(window.getSystemHandle());
        MakeKey::StepWindows();
    }
    return EXIT_SUCCESS;
}

Thanks for the help

In the code u provided u make a vector out of sf::RenderWindow. U could use pointers (sf::RenderWindow*) as one of solutions. In C++ the "=" is very vague to call "assigment operator" and this mostly used to explain stuff to beginners. Think of "=" as copy operator. It doesn't make sense to copy whole sf::RenderWindow, as u shown us

Jacob309

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Render Window Attempting to Reference deleted function
« Reply #2 on: March 21, 2020, 09:49:43 pm »
If I use pointers when I try to use .clear or .draw or any of those they get a error saying that it doesn't have that operator. I think there is a possibility i am declaring a am declaring or using the pointer wrong.

If there is a way to do something like push_back that doesn't copy that would be great.
« Last Edit: March 22, 2020, 12:26:37 am by Jacob309 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Render Window Attempting to Reference deleted function
« Reply #3 on: March 22, 2020, 07:34:28 am »
Problem already discussed here: https://en.sfml-dev.org/forums/index.php?topic=27030.0

Please avoid opening multiple posts for the same problem.
Laurent Gomila - SFML developer

 

anything