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

Author Topic: Class Function not being reckonized.  (Read 4027 times)

0 Members and 1 Guest are viewing this topic.

danger

  • Newbie
  • *
  • Posts: 3
    • View Profile
Class Function not being reckonized.
« on: March 20, 2014, 11:25:06 pm »
I think the subject is the problem at least. This is my code, it is from the book SFML Game Development. I think I am overlooking some simply aspect like a misplace function, but I have tried several things to no avail.

edit:Sorry forgot to mention the compiler error I am getting is "handlePlayerInput( etc...) has not been declared in this scope

Edit 2: I'll leave everything under here as it originally was. The solution that I figured out + based on the opinions of the others below was I was simply declaring the function incorrectly, thanks for those that helped. And to people who are new to SFML what I learned from this is sometimes it is a simple syntax error so be diligent in re-reading your code.

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class Game
{

    public:
                    Game();
      void          run();

    private:
        void        processEvents();
        void        update();
        void        render();
       
    private:
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;
};

int main ()
{
    Game game;
    game.run();
}

Game::Game()
: mWindow(sf::VideoMode (640, 480), "SFML APPLICATION")
,mPlayer()
{
    mPlayer.setRadius(40.f);
    mPlayer.setPosition(100.f, 100.f);
    mPlayer.setFillColor(sf::Color:: Cyan);
    }

void Game::run()
{
    while (mWindow.isOpen())
    {
        processEvents();
        update();
        render();
    }
}

void Game ::processEvents()
{
    sf::Event event;
    while (mWindow.pollEvent (event))
    {
        switch (event.type)
        {
        case sf::Event::KeyPressed:
            handlePlayerInput (event.key.code, true);
            break;
        case sf::Event::KeyReleased:
            handlePlayerInput(event.key.code, false);
            break:
        case sf::Event::Closed:
            mWindow.close();
        }
    }
}

void Game::update()
{
    sfVector2f movement (0.f, 0.);
    if (mIsMovingUp)
        movement.y -= 1.f;
    if  (mIsMovingdown)
        movement.y += 1.f;
    if (mIsMovingLeft)
        movement.x -= 1.f;
    if (mIsMovingRight)
        movement.x += 1.f;
}

void Game::render()
{
    mWindow.clear();
    mWindow.draw(mPlayer);
    mWindow.display();
}

void Game:: handlePlayerInput(sf::Keyboard::Key, key bool isPressed)
{
    if (key sf::Keyboard::w)
        mIsMovingUp = isPressed;
    else if (key sf::Keyboard::S)
        mIsMovingDown = isPressed;
    else if (key sf::Keyboard::A)
        mIsMovingLeft = isPressed;
    else if (key sf::Keyboard:: D)
        mIsMovingRight = isPressed;
}

Things I have tried unsuccessfully

  • I tried Declaring the handlePlayerInput() before the processEvent() in a void statement
  • I tried adding the function to the list in Class Game private.
  • I tried to add the handlePlayerIput() into the processEvents()

Thank you for anyone that takes time to help me with this, I feel like it is something simple that I am overlooking. I will continue to work on it on my own, and will edit this if I figure out a solution.
« Last Edit: March 21, 2014, 12:22:40 am by danger »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Class Function not being reckonized.
« Reply #1 on: March 20, 2014, 11:40:27 pm »
Do you have "handePlayerInput()" declared anywhere? :)

Declaring != defining

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Class Function not being reckonized.
« Reply #2 on: March 20, 2014, 11:42:55 pm »
Please use the [code=cpp]// Your Code[/code] when posting code.

The problem is, that handlePlayerInput was not declared in the class itself (there's not void handlePlayerInput(sf::Keyboard::Key, key bool isPressed); directly within the class Game).

If you're unable to solve such a basic problem, it might be best to take a step back and learn some more C++ basics. The SFML Game Development book is not a "How to programming in C++" book, instead it requires quite a bit of C++ knowledge to begin with. If you get a C++ book, make sure to pick a good one.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

danger

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Class Function not being reckonized.
« Reply #3 on: March 20, 2014, 11:50:33 pm »
Please use the [code=cpp]// Your Code[/code] when posting code.

The problem is, that handlePlayerInput was not declared in the class itself (there's not void handlePlayerInput(sf::Keyboard::Key, key bool isPressed); directly within the class Game).

If you're unable to solve such a basic problem, it might be best to take a step back and learn some more C++ basics. The SFML Game Development book is not a "How to programming in C++" book, instead it requires quite a bit of C++ knowledge to begin with. If you get a C++ book, make sure to pick a good one.

Thanks for the reply, first off I am learning the C++ language alongside with this, I do have a basic knowledge of language so far I am attempting to learn in tandem. As I said in the list at the bottom I did attempt to declare the function in class game and it still is giving me the same error.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Class Function not being reckonized.
« Reply #4 on: March 21, 2014, 12:00:06 am »
As I said in the list at the bottom I did attempt to declare the function in class game and it still is giving me the same error.
You should always show the code of what you actually have. ;)

Also there are some spaces right there, not sure if that's intended or just some c&p issue:
sf::Color:: Cyan
void Game:: handlePlayerInput(sf::Keyboard::Key, key bool isPressed)
void Game ::processEvents()

So again, please provide the cleaned up code (in code tags) that generate the error and provide the error message. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

danger

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Class Function not being reckonized.
« Reply #5 on: March 21, 2014, 12:20:31 am »
Quote
You should always show the code of what you actually have. ;)

Also there are some spaces right there, not sure if that's intended or just some c&p issue:
sf::Color:: Cyan
void Game:: handlePlayerInput(sf::Keyboard::Key, key bool isPressed)
void Game ::processEvents()

So again, please provide the cleaned up code (in code tags) that generate the error and provide the error message. :)

First off thank you for taking the time again to reply. I will do a better job in the future of posting the code properly so it isn't so confusing for others. I did figure out the issue. I was incorrectly initializing the function in the game class. Like I said I am learning C++ along side of SFmL. I only have about a 3 month headstart in C++ over SFML. I thought this error I was making was more of an oversight rather than a lack of knowledge (which it looks like it was,) Thanks again for the advice though.
« Last Edit: March 21, 2014, 12:23:00 am by danger »