I am reading a book about SFML and trying out the codes in the book. but "ActionTarget" template class keeps giving me errors when I am trying to use the class in another class.
the class was in two different files(.h and .cpp). then I tried putting them in .h file. but still giving the same error.
this is "ActionTarget" template class:
#ifndef ACTIONTARGET_H
#define ACTIONTARGET_H
#include <SFML/Graphics.hpp>
#include "ActionMap.h"
template <typename T= int>
class ActionTarget
{
public:
ActionTarget(const ActionTarget<T>&)= delete;
ActionTarget<T>& operator= (const ActionTarget<T>&) = delete;
using FuncType = std::function<void(const sf::Event&)>;
ActionTarget(const ActionMap<T>& map);
bool processEvent(const sf::Event& ) const;
void processEvents()const;
void bind(const T&, const FuncType&);
void unbind(const T&);
private:
std::list<std::pair<T, FuncType>> _eventsRealTime;
std::list<std::pair<T, FuncType>> _eventsPoll;
const ActionMap<T>& _actionMap;
};
/**ActionTarget::ActionTarget()
{
}
*/
template<typename T>
bool ActionTarget<T>::processEvent(const sf::Event& event) const
{
bool res = false;
for(auto& action: _eventsPoll)
{
if(action.first == event){
action.second(event);
res = true;
break;
}
}
return res;
}
template<typename T>
void ActionTarget<T>::processEvents()const
{
for (auto& action: _eventsRealTime)
{
if(action.first.test())
action.second(action.first._event);
}
}
template<typename T>
void ActionTarget<T>::bind(const T& key, const FuncType& callback)
{
if (key._type& Action::Type::RealTime)
_eventsRealTime.emplace_back(key, callback);
else
_eventsPoll.emplace_back(key, callback );
}
template<typename T>
void ActionTarget<T>::unbind(const T& key)
{
auto remove_func = [&key](const std::pair<T, FuncType>& pair)-> bool
{
return pair.first == key;
};
if (key._type& Action::Type::RealTime)
_eventsRealTime.remove_if(remove_func);
else
_eventsPoll.remove_if(remove_func);
}
#endif // ACTIONTARGET_H
And this is the "Player.h" that inherits from "ActionTarget"
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include "ActionMap.h"
#include "ActionTarget.h"
class Player : public sf::Drawable, public ActionTarget<int> {
public:
Player(const Player&) = delete;
Player& operator= (const Player&) = delete;
Player();
void processEvents();
template<typename ... Args>
void setPosition(Args&& ...args){
_shape.setPosition(std::forward<Args>(args)...);
}
void update(sf::Time deltaTime);
enum PlayerInputs{Up, Left, Right};
static void setDefaultsInputs();
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
sf::RectangleShape _shape;
sf::Vector2f _velocity;
bool _isMoving;
int _rotation;
static ActionMap<int> _playerInputs;
};
#endif // PLAYER_H
this is Player.cpp
#include "Player.h"
#define M_PI 3.14
#include "ActionMap.h"
#include "ActionTarget.h"
ActionTarget<int> action_target;
void Player::processEvents(){
_isMoving = false;
_rotation = 0;
action_target.processEvents();
}
Player::Player() : ActionTarget(_playerInputs)
,_shape(sf::Vector2f(32,32))
,_isMoving(false)
,_rotation(0)
{
_shape.setFillColor(sf::Color::Blue);
_shape.setOrigin(16,16);
bind(PlayerInputs::Up, [this](const sf::Event&){ _isMoving = true;});
bind(PlayerInputs::Left, [this](const sf::Event&){_rotation -=1;});
bind(PlayerInputs::RIght, [this](const sf::Event&){_rotation += 1;});
}
void Player::update(sf::Time deltaTime)
{
float seconds = deltaTime.asSeconds();
if(_rotation != 0)
{
float angle = _rotation *180*seconds;
_shape.rotate(angle);
}
if (_isMoving)
{
float angle = _shape.getRotation()/180*M_PI - M_PI/2;
_velocity += sf::Vector2f(std::cos(angle), std::sin(angle))*60.f*seconds;
}
_shape.move(seconds*_velocity);
}
void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(_shape, states);
}
void Player::setDefaultsInputs(){
_playerInputs.map(PlayerInputs::Up, Action(sf::Keyboard::Up));
_playerInputs.map(PlayerInputs::Right, Action(sf::Keyboard::Right));
_playerInputs.map(PlayerInputs::Left, Action(sf::Keyboard::Left));
}
ActionMap<int> Player::_playerInputs;
These are the errors:
> ||=== Build: Debug in sfml (compiler: GNU GCC Compiler) ===|
> F:\C++\sfml\ActionTarget.h|7|error: 'ActionTarget' is not a template type|
> F:\C++\sfml\ActionTarget.h|36|error: expected initializer before '<' token|
> F:\C++\sfml\ActionTarget.h|51|error: expected initializer before '<' token|
> F:\C++\sfml\ActionTarget.h|7|error: 'ActionTarget' is not a template type|
> F:\C++\sfml\ActionTarget.h|36|error: expected initializer before '<' token|
> F:\C++\sfml\ActionTarget.h|51|error: expected initializer before '<' token|
> F:\C++\sfml\ActionTarget.h|61|error: expected initializer before '<' token|
> F:\C++\sfml\ActionTarget.h|72|error: expected initializer before '<' token|
> F:\C++\sfml\Player.h|9|error: expected template-name before '<' token|
> F:\C++\sfml\Player.h|9|error: expected '{' before '<' token|
> F:\C++\sfml\Player.h|9|error: expected unqualified-id before '<' token|
> F:\C++\sfml\ActionTarget.h|61|error: expected initializer before '<' token|
> F:\C++\sfml\ActionTarget.h|72|error: expected initializer before '<' token|
> F:\C++\sfml\Player.h|9|error: expected template-name before '<' token|
> F:\C++\sfml\Player.h|9|error: expected '{' before '<' token|
> F:\C++\sfml\Player.h|9|error: expected unqualified-id before '<' token|
> ||=== Build failed: 16 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Aucun commentaire:
Enregistrer un commentaire