0
\$\begingroup\$

I am trying to write sf::mouse::getPosition(const Window &Relative To) in a function but I can't write the correct argument in it. Why is that?

Here is my Game.cpp File: EDIT: I pasted the current code:

#include "Game.h" Game::Game() : m_window("Multiplayer Space Shooter Game", sf::Vector2u(800, 600)) { // Setting up class members. m_shipText.loadFromFile("C:\\Users\\AliTeo\\Desktop\\Piksel çalışmaları\\ship_pixel2.png"); m_ship.setTexture(m_shipText); m_ship.setOrigin(m_shipText.getSize().x / 2, m_shipText.getSize().y / 2); m_ship.setPosition(320, 240); } Game::~Game() {} void Game::Update() { m_window.Update(); // Update window events. m_ship.move(m_speedX, m_speedY); m_ship.setRotation(90 + m_angle); HandleInput(); } void Game::HandleInput() { sf::Mouse m_mouse; m_angle = atan2(m_mouse.getPosition(&m_window).y - m_ship.getPosition().y, m_mouse.getPosition(&m_window).x - m_ship.getPosition().x); m_angle *= 180 / m_PI; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && m_speedX <= m_speedLimit) { m_speedX += m_acceleration; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_speedX >= -1 * m_speedLimit) { m_speedX -= m_acceleration; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && m_speedY <= m_speedLimit) { m_speedY += m_acceleration; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && abs(m_speedY) >= -1 * m_speedLimit) { m_speedY -= m_acceleration; } } void Game::Render() { m_window.BeginDraw(); // Clear. m_window.Draw(m_ship); m_window.EndDraw(); // Display. } Window* Game::GetWindow() { return &m_window; } 

m_window is a member of Game.h

no instance of overloaded function "sf::Mouse::getPosition" matches the argument list #include "Window.h" class Game { public: Game(); ~Game(); void HandleInput(); void Update(); void Render(); Window* GetWindow(); private: sf::Sprite m_ship; sf::Texture m_shipText; //sf::Vector2i mousePos = sf::Mouse::getPosition(); Window m_window; const float m_PI = 3.1415f; float m_speedX = 0; float m_speedY = 0; float m_acceleration = 0.001f; //float m_friction = 0.001f; float m_speedLimit = 0.5f; float m_angle; }; 
\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

The getPosition method inside of the sf::Mouse class is actually static. You'll need to use it like this inside of your game loop that preforms updates every frame:

sf::Vector2i mousePos = sf::Mouse::getPosition(m_window); 
\$\endgroup\$
1
  • \$\begingroup\$ Hi, I tried to write this inside main.cpp then Game::Update but I am still getting the same error. Where should I write it exactly? \$\endgroup\$ Commented Mar 1, 2018 at 5:21
0
\$\begingroup\$

You forgot to pass m_window to the second Mouse::getPosition

\$\endgroup\$
3
  • \$\begingroup\$ No; that is not the case, it still gives error. \$\endgroup\$ Commented Mar 1, 2018 at 13:51
  • \$\begingroup\$ Can you paste your current code into pastebin? @AliTeo \$\endgroup\$ Commented Mar 1, 2018 at 14:56
  • \$\begingroup\$ I have written the full code since it is not too long. \$\endgroup\$ Commented Mar 1, 2018 at 17:28
0
\$\begingroup\$

You're trying to pass a pointer to m_window but the function just wants a reference.

m_mouse.getPosition(m_window) should do the trick as opposed to your current m_mouse.getPosition(&m_window).

\$\endgroup\$
3
  • \$\begingroup\$ Hi, I tried writing that, but didn't work. \$\endgroup\$ Commented Mar 1, 2018 at 17:52
  • \$\begingroup\$ What is the error you get? \$\endgroup\$ Commented Mar 1, 2018 at 20:30
  • \$\begingroup\$ no instance of overloaded function "sf::Mouse::getPosition" matches the argument list. \$\endgroup\$ Commented Mar 2, 2018 at 6:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.