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

Author Topic: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition  (Read 1614 times)

0 Members and 1 Guest are viewing this topic.

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
Hi,

I am trying to understand std::functions, so I tried the following:

auto bp = sf::Mouse::isButtonPressed;

and that works fine.

I tried the something similar:

auto gp = sf::Mouse::getPosition;

and it gave me the error
unable to deduce 'auto' from 'sf::Mouse::getPosition'

when I switched
auto
to
std::function<sf::Vector2i(const sf::Window&)>

I got
error: conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<sf::Vector2<int>(const sf::Window&)>' requested

What am I doing wrong? I am sure, I am misunderstanding this whole concept, but I find it wierd, that the first one worked without a problem and the other one didn't. Is it because of the overload?
« Last Edit: October 23, 2016, 06:06:03 pm by kim366 »

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
« Reply #1 on: October 23, 2016, 06:41:23 pm »
There are 2 possible overloads of that function and compiler couldn't deduce which one to use. Hover I'm not really sure how to specify that, maybe some casting could make it work?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
« Reply #2 on: October 23, 2016, 07:18:00 pm »
We like to keep this forum focused on questions related to SFML. There are many other sites out there that focus on C++ in general (e.g. StackOverflow).

Additionally you should consult a C++ resource (such as a book, C++ reference or online article) when trying to learn a new language feature. C++ just can't be learned by trial and error (and asking others to spend their time explaining things to you).

Do you know what the syntax of std::function is? What shoould be put between the <>? What can std::function hold? How do you call a function held by std::function?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
« Reply #3 on: October 24, 2016, 02:43:27 pm »
Okay, sorry about that. I just didn't want to ask it there, because from my own experimentation everything else worked, but I just overlooked the const before the sf::Window.

Have a nice day!