【问题标题】:std::bind causes illegal indirection errorstd::bind 导致非法间接错误
【发布时间】:2015-01-27 02:57:37
【问题描述】:

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

数据表.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

数据表.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

飞机.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>          ]

任何可能的解决方案将不胜感激。

【问题讨论】:

  • SSCCE 请。在不了解所涉及的函数签名和类型的情况下,我们应该如何提供帮助?
  • 您的示例仍然既不独立也不可编译。 PickupData::action 的类型是什么?以后如何调用action
  • 好的,我又编辑了一遍。对不起,昨天我原来打字的时候真的很累。无论如何,如果您需要更多信息,请告诉我。
  • 请尝试生成一个简单的示例,而不是这个代码转储。当然,您只需几行代码即可重现相同的错误。
  • Gerald 找到了解决问题的方法,但如果我需要提出其他问题,我会记住您的建议。

标签: c++ compiler-errors sfml stdbind


【解决方案1】:

所以问题出在这一行:

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

根据错误转储,actionstd::function&lt;void(Aircraft&amp;)&gt;,但 repair 是基类 Entity 的成员,而不是 Aircraft。由于std::bind 返回的返回值很奇怪,它不能直接转换为std::function&lt;void(Aircraft&amp;)&gt;。您首先需要将返回值转换为std::function&lt;void(Entity&amp;)&gt;,在将其分配给操作之前,可以隐式转换为std::function&lt;void(Aircraft&amp;)&gt;。即:

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

【讨论】:

  • 占位符是this指针,因为initializePickupData函数不是Aircraft的成员,所以它不能使用this指针本身。很抱歉我之前没有说清楚。
  • @MasterKulon 我认为这是一个 VisualStudio 错误。您的代码在 gcc 上编译,如果我进行以下更改 std::function&lt;void(Aircraft*)&gt; action; Table[static_cast<int>(type)].action(&player);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2016-05-01
  • 2013-02-01
相关资源
最近更新 更多