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

Author Topic: Error, window.draw  (Read 1608 times)

0 Members and 1 Guest are viewing this topic.

paul.mihaita

  • Newbie
  • *
  • Posts: 16
    • View Profile
Error, window.draw
« on: March 14, 2013, 05:15:09 pm »
Hey guys, i'm new at sfml. I want to draw 2 images but i get 2 errors:
25|error: 'desenbackground' was not declared in this scope|
26|error: 'desenpatrat' was not declared in this scope|

here is the code

#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    while (window.isOpen())
    {
         sf::Texture texture;
         if (!texture.loadFromFile("Background.png"))
            return EXIT_FAILURE;
         sf::Sprite desenbackground(texture);
         if (!texture.loadFromFile("Patrat.png"))
            return EXIT_FAILURE;
         sf::Sprite desenpatrat(texture);
    }
    while (window.isOpen())
     {
         sf::Event event;
         while (window.pollEvent(event))
         {
             if (event.type == sf::Event::Closed)
                 window.close();
         }

         window.clear();
         window.draw(desenbackground);
         window.draw(desenpatrat);
         window.display();
     }

     return EXIT_SUCCESS;
 }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Error, window.draw
« Reply #1 on: March 14, 2013, 05:23:16 pm »
Welcome to SFML! :)

For posting C++ code here on the forum, you should always use the code=cpp tag.

As for your problem, the compiler tells you exactly what the problem is. desenbackground and desenpatrat are not defined in the scope you're trying to use them in.
If you don't know/understand what a scope is in C++, then you've missed some major part of C++ itself and I advise you to first learn C++ to get a good understand of the language. Here's a list of recommended and good C++ books. ;)

You basically define the two variables (including the textures) in the scope of your first while-loop, but as soon as you get to the end of the loop the variables will get destroyed and either recreated for the next iteration or the code will move on.
Do you even know what you're doing here? Because the first while loop is completely useless and misplaced. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

paul.mihaita

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Error, window.draw
« Reply #2 on: March 14, 2013, 05:26:25 pm »
Sorry...I know c++ but i didn't noticed that while:|...Sry again for my post