#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0

    C++, error: not declared in this scope


    Hello, I'm trying to create a space invaders game and I'm having some problems with my functions. On the line containing: myBeem.setBeemLocation(50, 50);
    I get the following error: 'myBeem' was not declared in this scope

    Why?

    cheers

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

    Code:
    #include <SFML/Graphics.hpp>
    #include <SFML/Window.hpp>
    #include <iostream>
    #include <SFML/Audio.hpp>
    
    using namespace std;
    
    //WINDOW
    sf::VideoMode VMode(800, 600, 32);
    sf::RenderWindow Window(VMode, "Space Defender");
    
    
    class laserbeem{
    
    public:
        laserbeem();
        ~laserbeem();
        void setBeemLocation(float x, float y);
    
    protected:
        float beemLocationX;
        float beemLocationY;
    
    };
    
    laserbeem::laserbeem(){
    }
    
    laserbeem::~laserbeem(){
    }
    
    void laserbeem::setBeemLocation(float x, float y){
    
        beemLocationX = x;
        beemLocationY = y;
    }
    
    
    int main()
    {
    
    
        //MUSIC
        sf::Music music;
        if (!music.openFromFile("music.ogg"))
            std::cout << "Music failed to load!" << std::endl;
        music.play();
    
        //SOUNDS
        sf::SoundBuffer buffer;
        if (!buffer.loadFromFile("laser.ogg"))
            std::cout << "Laser failed to load!" << std::endl;
        //return 0;
    
        sf::Sound laser;
        laser.setBuffer(buffer);
    
    
        //BACKGROUND
        sf::Texture Image;
        if (!Image.loadFromFile("gal.jpg"))
        {
                std::cout << "Image failed to load!" << std::endl;
                return 1;
        }
    
        sf::Sprite Sprite;
        Sprite.setTexture(Image);
    
        Sprite.setPosition(0.0f, 0.0f);
        Sprite.setScale((float)VMode.width/(float)Sprite.getTexture()->getSize().x, (float)VMode.height/(float)Sprite.getTexture()->getSize().y);
    
        //Sprite.setScale((float)VMode.width, (float)VMode.height);
        //Sprite.setPosition(100.0f, 30.0f);
        //Sprite.setScale(.5f, .3f);
    
        //SHIP
        sf::Texture Image2;
        if (!Image2.loadFromFile("ship.png"))
            return 1;
    
        sf::Sprite Sprite2;
        Sprite2.setTexture(Image2);
    
        //float x = 400.0;
        Sprite2.setPosition(400.0f, 450.0f);
        Sprite2.setScale(.8f, .8f);
    
    
    while (Window.isOpen())
    {
        sf::Event Event;
        while (Window.pollEvent(Event))
        {
            switch (Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                std::cout << "The window was closed." << std::endl;
                break;
    
            case sf::Event::KeyPressed:
                //LEFT KEY
                if (Event.key.code == sf::Keyboard::Left)
                Sprite2.move(-15,0);
                //RIGHT KEY
                if (Event.key.code == sf::Keyboard::Right)
                Sprite2.move(15,0);
                //UP KEY
                if (Event.key.code == sf::Keyboard::Up)
                Sprite2.move(0,-15);
                //DOWN KEY
                if (Event.key.code == sf::Keyboard::Down)
                Sprite2.move(0,15);
                //LEFT CTRL KEY
                if (Event.key.code == sf::Keyboard::LControl)
                laserbeem myBeem;
                myBeem.setBeemLocation(50, 50);
                laser.play();
            break;
    
            default:
                break;
            }
        }
    
        Window.clear(sf::Color(144, 144, 144));
        Window.draw(Sprite);
        Window.draw(Sprite2);
        Window.display();
    }
    
        return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Block scope. myBeem was declare inside the code block of the if statement, so it only exists there. After you leave that code block, it no longer exists.

    Or did you intend to enclose two or three of the lines following that if statement in the same code block? You would need to use braces in that case:
    Code:
                if (Event.key.code == sf::Keyboard::LControl)
                {
                    laserbeem myBeem;
                    myBeem.setBeemLocation(50, 50);
                    laser.play();
                }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    Block scope. myBeem was declare inside the code block of the if statement, so it only exists there. After you leave that code block, it no longer exists.

    Or did you intend to enclose two or three of the lines following that if statement in the same code block? You would need to use braces in that case:
    Code:
                if (Event.key.code == sf::Keyboard::LControl)
                {
                    laserbeem myBeem;
                    myBeem.setBeemLocation(50, 50);
                    laser.play();
                }
    That solved the problem. Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo