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

Author Topic: Trying to use RenderWindow from a function, but keep getting error (code inside)  (Read 3709 times)

0 Members and 1 Guest are viewing this topic.

RetroRain

  • Newbie
  • *
  • Posts: 14
    • View Profile
I'm trying to use SFML's RenderWindow class from another function, but I keep getting an error, and honestly, I have no clue why I am getting the error.  I'm referencing the "myWindow" variable in the parameter list, and everything has been declared.  I'm getting frustrated.  I've Googled tons of tutorials and articles, and I can't pinpoint why this error is happening.  Here's the code:

#include <SFML/Graphics.hpp>
#include "declarations.h"

int main()
{
    //create the main window
    sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Title");

    //start the game loop
    while (window.isOpen())
    {
        //process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            //close window: exit
            if (event.type == sf::Event::Closed) window.close();
        }
        windowHandler(myWindow);  //Right here is where I am getting the error
    }
    return EXIT_SUCCESS;
}

-----------------------------------


#ifndef DECLARATIONS_H_
#define DECLARATIONS_H_

const short SCREEN_WIDTH = 256;
const short SCREEN_HEIGHT = 240;

void windowHandler(sf::RenderWindow& myWindow);

#endif

-----------------------------------


#include <SFML/Graphics.hpp>
#include "declarations.h"

void windowHandler(sf::RenderWindow& myWindow)
{
    //clear screen
    myWindow.clear();

    //draw to screen, will update this later, right now it doesn't draw anything
    //window.draw();

    //update the window
    myWindow.display();
}

The error I get:

In function 'int main()':|
error: 'myWindow' was not declared in this scope|

I don't understand why I am getting this error.  Everything is declared, and myWindow is referenced with an &.  The function prototype is included in the declarations header file, and the header file is included before it even touches main(), which means it should be able to be used anywhere in the program.  So why is it telling me that 'myWindow' is not declared in this scope.  It is!

If you know the answer to the problem, it would greatly be appreciated.  Thank you so much.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
As the error mentions, you have not declared a 'myWindow' in the main function. In the main function you instead called your variable just 'window', so that's what you should be passing into windowHandler function.

The name of the variable you pass into a function doesn't have to be the same name as the that function's argument.

RetroRain

  • Newbie
  • *
  • Posts: 14
    • View Profile
I understand now, at least I think I do.  When I'm creating an instance of RenderWindow, I'm using the name 'window'.  So, it is that 'window' that has to be passed to my newly created function.

I tried using a different variable name to pass to the function, such as 'a', or 'i', but it gives me errors.  However, when I use 'window', such as windowHandler(window);, it works like a charm.  And the only thing I can think of, is that the only instance of window is 'window'.  So it works.

In any case, I got it to work, so thank you very much. :)