user2547918
user2547918

Reputation: 1

c++, not declared in this scope

I'm trying to create a 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


#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;
}

Upvotes: 0

Views: 1441

Answers (2)

Jashaszun
Jashaszun

Reputation: 9270

In your big switch statement, you should give the case statement containing the line with your error its own block, like so:

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;
}

And, just saying, 'beem' should be spelled 'beam'.

Upvotes: -1

NPE
NPE

Reputation: 500913

The following:

    if (Event.key.code == sf::Keyboard::LControl)
    laserbeem myBeem;
    myBeem.setBeemLocation(50, 50);

is equivalent to:

    if (Event.key.code == sf::Keyboard::LControl) {
      laserbeem myBeem;
    }
    // myBeem no longer exists
    myBeem.setBeemLocation(50, 50);

In other words, the scope of myBeem is limited to the block inside the if statement.

You need to revisit the placement of curly braces in your code (along with the use of indentation, to improve readability).

Upvotes: 12

Related Questions