I have a class:
class InputMap
{
public:
template<typename Function>
void setHotkey(sf::Keyboard::Key hotkey, Function onClick)
{
map[hotkey] = onClick;
}
void triggerHotkey(sf::Keyboard::Key hotkey)
{
if(map.find(hotkey) != map.end())
map[hotkey]();
}
protected:
std::map <sf::Keyboard::Key, std::function<void()>> map;
};
and when calling the setHotkey function like so:
setHotkey(sf::Keyboard::Left, [=](){TestActor->move(sf::Vector2f(-20, 0));});
setHotkey(sf::Keyboard::Right, [=](){TestActor->move(sf::Vector2f(20, 0));});
I get these errors:
../Tyrant/include/Framework/InputMap.hpp|14|error: ‘void TGE::InputMap::setHotkey(sf::Keyboard::Key, Function) [with Function = TGE::State::setHotkey(sf::Keyboard::Key, Function) [with Function = TestState::enter()::__lambda2]::__lambda1]’, declared using local type ‘TGE::State::setHotkey(sf::Keyboard::Key, Function) [with Function = TestState::enter()::__lambda2]::__lambda1’, is used but never defined [-fpermissive]|
../Tyrant/include/Framework/InputMap.hpp|14|error: ‘void TGE::InputMap::setHotkey(sf::Keyboard::Key, Function) [with Function = TGE::State::setHotkey(sf::Keyboard::Key, Function) [with Function = TestState::enter()::__lambda3]::__lambda1]’, declared using local type ‘TGE::State::setHotkey(sf::Keyboard::Key, Function) [with Function = TestState::enter()::__lambda3]::__lambda1’, is used but never defined [-fpermissive]|
||=== Build finished: 2 errors, 1 warnings ===|
Now I'm guessing I could just compile with -fpermissive but I would like to avoid doing so.
EDIT:
Apparently the error was because the .cpp file for InputMap contained
template<typename Function>
void setHotkey(sf::Keyboard::Key hotkey, Function onClick)
{
map[hotkey] = onClick;
}
and the header was
template<typename Function>
void setHotkey(sf::Keyboard::Key hotkey, Function onClick);
So I guess it doesn't like the declaration and implementation to be in different files, possibly because of the template? Is there a proper way to do this or am I supposed to have it in the header only?