std :: bind导致非法间接错误

时间:2015-01-27 02:57:37

标签: c++ compiler-errors sfml stdbind

我正在阅读SFML游戏开发书,但我遇到了std :: bind的问题。我搜索了一个解决方案,似乎其他人也有类似的问题。但是,我仍然无法找到解决此特定问题的方法。这是我的代码:

DataTables.hpp

#ifndef DATA_TABLES_HPP
#define DATA_TABLES_HPP

#include <functional>
#include <vector>

struct PickupData {
    std::function<void(Aircraft&)> action;
}

std::vector<PickupData> initializePickupData();

#endif

DataTables.cpp

using namespace std::placeholders;

std::vector<PickupData> initializePickupData() {
    std::vector<PickupData> data(static_cast<int>(Pickup::Type::TypeCount));

    data[static_cast<int>(Pickup::Type::HealthRefill)].action = std::bind(&Aircraft::repair, _1, 25);

    data[static_cast<int>(Pickup::Type::MissileRefill)].action = std::bind(&Aircraft::collectMissiles, _1, 3);

    data[static_cast<int>(Pickup::Type::FireSpread)].action = std::bind(&Aircraft::increaseSpread, _1);

    data[static_cast<int>(Pickup::Type::FireRate)].action = std::bind(&Aircraft::increaseFireRate, _1);

    return data;
}

Entity.hpp

#ifndef ENTITY_HPP
#define ENTITY_HPP

class Entity {
    public:
        explicit Entity(int hitpoints);

        void repair(int points);

    protected:
        int hitpoints;
};

#endif

Entity.cpp

#include "Entity.hpp"

#include <cassert>

Entity::Entity(int hitpoints)
    : hitpoints(hitpoints)
{}

void Entity::repair(int points) {
    assert(points > 0);

    hitpoints += points;
}

Aircraft.hpp

#ifndef AIRCRAFT_HPP
#define AIRCRAFT_HPP

class Aircraft : public Entity {
    public:
        Aircraft();

        void increaseFireRate();
        void increaseSpread();
        void collectMissiles(unsigned int count);

    private:
        int missileAmmo;
        int fireRateLevel;
        int spreadLevel;
}

#endif

Aircraft.cpp

#include "Aircraft.hpp"

Aircraft::Aircraft()
    : Entity(100)
    , missileAmmo(2)
    , fireRateLevel(1)
    , spreadLevel(1)
{}

void Aircraft::collectMissiles(unsigned int count) {
    missileAmmo += count;
}

void Aircraft::increaseSpread() {
    if (spreadLevel < 3)
        ++spreadLevel;
}

void Aircraft::increaseFireRate() {
    if (fireRateLevel < 10)
        ++fireRateLevel;
}

Pickup.hpp

#ifndef PICKUP_HPP
#define PICKUP_HPP

#include "Entity.hpp"
#include "Aircraft.hpp"

class Pickup : public Entity {
    public:
        enum class Type {
            HealthRefil,
            MissileRefill,
            FireSpread,
            FireRate,
            TypeCount
        }

        explicit Pickup(Type type);

        void apply(Aircraft& player) const;

    private:
        Type type;
}

#endif

Pickup.cpp

#include "Pickup.hpp"

#include "DataTables.hpp"

namespace {
    const std::vector<PickupData> Table = initializePickupData();
}

Pickup::Pickup(Type type)
    : Entity(1)
    , type(type)
{}

void Pickup::apply(Aircraft& player) const {
    Table[static_cast<int>(type)].action(player);
}

这是错误:

1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1241): error C2100: illegal indirection
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149) : see reference to function template instantiation '_Rx std::_Pmf_wrap<void (__thiscall Entity::* )(int),_Rx,Entity,int>::operator ()<Aircraft>(_Wrapper &,int) const' being compiled
1>          with
1>          [
1>              _Rx=void
1>  ,            _Wrapper=Aircraft
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149) : see reference to function template instantiation '_Rx std::_Pmf_wrap<void (__thiscall Entity::* )(int),_Rx,Entity,int>::operator ()<Aircraft>(_Wrapper &,int) const' being compiled
1>          with
1>          [
1>              _Rx=void
1>  ,            _Wrapper=Aircraft
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>::_Do_call<Aircraft,0,1>(std::tuple<Aircraft &>,std::_Arg_idx<0,1>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>::_Do_call<Aircraft,0,1>(std::tuple<Aircraft &>,std::_Arg_idx<0,1>)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(283) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>::operator ()<Aircraft&>(Aircraft &)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(283) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>::operator ()<Aircraft&>(Aircraft &)' being compiled
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<std::_Bind<true,_Ret,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>,false>::_ApplyX<_Rx,Aircraft&>(Aircraft &)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Rx=void
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<std::_Bind<true,_Ret,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>,false>::_ApplyX<_Rx,Aircraft&>(Aircraft &)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Rx=void
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(226) : while compiling class template member function 'void std::_Func_impl<_MyWrapper,_Alloc,_Ret,Aircraft &>::_Do_call(Aircraft &)'
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Func_class<void,Aircraft &>>
1>  ,            _Ret=void
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(495) : see reference to class template instantiation 'std::_Func_impl<_MyWrapper,_Alloc,_Ret,Aircraft &>' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Func_class<void,Aircraft &>>
1>  ,            _Ret=void
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,Aircraft &>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Ty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Alloc=std::allocator<std::_Func_class<void,Aircraft &>>
1>  ,            _Fty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,Aircraft &>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Ty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Alloc=std::allocator<std::_Func_class<void,Aircraft &>>
1>  ,            _Fty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,Aircraft &>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,Aircraft &>>>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Ty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Fty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Alloc=std::allocator<std::_Func_class<void,Aircraft &>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,Aircraft &>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,Aircraft &>>>(_Fty &&,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Ty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Fty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Alloc=std::allocator<std::_Func_class<void,Aircraft &>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(688) : see reference to function template instantiation 'void std::_Func_class<_Ret,Aircraft &>::_Reset<_Ty>(_Fty &&)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Ty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Fty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(688) : see reference to function template instantiation 'void std::_Func_class<_Ret,Aircraft &>::_Reset<_Ty>(_Fty &&)' being compiled
1>          with
1>          [
1>              _Ret=void
1>  ,            _Ty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>  ,            _Fty=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>          ]
1>          c:\users\alec\desktop\c++ code\sfmlproject\sfmlproject\datatables.cpp(78) : see reference to function template instantiation 'std::function<void (Aircraft &)> &std::function<void (Aircraft &)>::operator =<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>>(_Fx &&)' being compiled
1>          with
1>          [
1>              _Fx=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>          ]
1>          c:\users\alec\desktop\c++ code\sfmlproject\sfmlproject\datatables.cpp(78) : see reference to function template instantiation 'std::function<void (Aircraft &)> &std::function<void (Aircraft &)>::operator =<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>>(_Fx &&)' being compiled
1>          with
1>          [
1>              _Fx=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Entity::* )(int),void,Entity,int>,std::_Ph<1> &,int>
1>          ]

非常感谢任何可能的解决方案。

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

data[static_cast<int>(Pickup::Type::HealthRefill)].action = 
  std::bind(&Aircraft::repair, _1, 25);

根据错误转储,actionstd::function<void(Aircraft&)>,但repair是基类Entity的成员,而不是Aircraft。由于std::bind返回的时髦返回值,无法直接转换为std::function<void(Aircraft&)>。首先需要将返回值强制转换为std::function<void(Entity&)>,在将其赋值给操作之前,可以隐式转换为std::function<void(Aircraft&)>。即:

data[static_cast<int>(Pickup::Type::HealthRefill)].action = 
  (std::function<void(Entity &)>) std::bind(&Aircraft::repair, std::placeholders::_1, 25);