提升Python签名不匹配问题

时间:2011-06-06 21:54:47

标签: c++ python boost-python

我的签名不匹配我正在暴露给python的方法。这是下面的代码

class StatusEffect
{
public:
   //Virtual functions and non related ommitted
   void AddCancellation(std::string &effect)
   {
       CancelsEffects.insert(effect);
   }
}

我想在我的python脚本中覆盖StatusEffect中的一些虚拟方法,所以我创建了一个类包装器。包装器不会以任何方式覆盖或更改AddCancellation,我只是简单地显示它以标记

struct StatusEffectWrapper : Game::StatusEffects::StatusEffect
{
   //Wrappings for virtual functions here. I don't touch AddCancellation
}

我将它暴露给python如下:

 class_<StatusEffects::StatusEffect, boost::noncopyable, std::auto_ptr<StatusEffectWrapper> >("StatusEffect")
    .def("AddCancellation", &StatusEffect::AddCancellation)
    ;

在我的python文件中,我像这样扩展了基本的StatusEffect类:

class StatCreepEffect(StatusEffect):
    def __init__(self, name, stat_, rate_, descript, uid, turns, prior, canrefresh, poseffect, cats, conds, flgs, sep, pers, cancels):
        StatusEffect.__init__(self, name, descript, uid, turns, prior, canrefresh, poseffect, cats, conds, flgs, sep, pers)
        self.rate = rate_
        self.stat = stat_
        self.PushCancellations(cancels)
    def PushCancellations(self, cancels):
        for cancel in cancels:
            print (cancel)
            self.AddCancellation(cancel)

Regen = StatCreepEffect("Regen", Stat.HP, 0.05, "Regenerates a small amount of HP every turn", PrimaryEngine.GetUID(), 14, 4, True, True, StatusCategories.SC_Regen, 0, StatusFlags.TickEffect, True, StatusPersistence.SP_Timer_Cure, ["Poison", "Poison-"])

然而,当我这样做时,我收到以下错误:

Poison
Traceback (most recent call last):
  File "..\..\..\Resources\Scripts\StatusEffects.py", line 27, in <module>
    Regen = StatCreepEffect("Regen", Stat.HP, 0.05, "Regenerates a small amount
of HP every turn", PrimaryEngine.GetUID(), 14, 4, True, True, StatusCategories.S
C_Regen, 0, StatusFlags.TickEffect, True, StatusPersistence.SP_Timer_Cure, ["Poi
son", "Poison-"])
  File "..\..\..\Resources\Scripts\StatusEffects.py", line 11, in __init__
    self.PushCancellations(cancels)
  File "..\..\..\Resources\Scripts\StatusEffects.py", line 23, in PushCancellati
ons
    self.AddCancellation(cancel)
Boost.Python.ArgumentError: Python argument types in
    StatusEffect.AddCancellation(StatCreepEffect, str)
did not match C++ signature:
    AddCancellation(class Game::StatusEffects::StatusEffect {lvalue}, class std:
:basic_string<char,struct std::char_traits<char>,class std::allocator<char> > {l
value})

我不完全确定问题是什么。 StatCreepEffect应该是StatusEffect的一个实例应该不是吗?而Boost :: python自动处理字符串的转换不是吗?我是否错误地调用了该方法?感觉它是我错过的小东西,但我不确定它是什么。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

原来我需要

void AddCancellation(std::string &effect)
{
   CancelsEffects.insert(effect);
}

作为

void AddCancellation(const std::string &effect)
{
   CancelsEffects.insert(effect);
}

答案 1 :(得分:1)

我认为问题可能是Boost.Python并不完全理解StatusEffectWrapper是从StatusEffect派生的。除非你有一些原因要指定保持类型,否则我会尝试将类指定为:

class_<StatusEffects::StatusEffectWrapper, boost::noncopyable>("StatusEffect")
    .def("AddCancellation", &StatusEffectWrapper::AddCancellation)
    ;