0

我有以下设置,这让我很头疼,因为我没有看到问题:

ValidationModule module = ValidationModule(std::vector<FilterFlag>(0));
Settings validationSettings = Settings("Validation Settings");
validationSettings.registerFloat("test", module, &ValidationModule::setRatio, &ValidationModule::getRatio, 0.0f, 1.0f);

registerFloat有两个指向成员函数的指针ValidationModule

验证模块.h

// constructor
ValidationModule(const std::vector<FilterFlag>& filterFlags, float ratio = 0.7f);

// getter and setter, which are passed to Settings::registerFloat
inline void setRatio(float value) { _ratio = value; }
inline float getRatio() const { return _ratio; }

设置.h

template<class TClass>
inline void Settings::registerFloat(const std::string& name, TClass owner, void (TClass::*setter)(float), float (TClass::*getter)() const, float minValue, float maxValue)
{
    CallBackToMemberSingleArg<float, TClass>* setterCallback = new CallBackToMemberSingleArg<float, TClass>(owner, setter);
    CallBackToMemberReturn<float, TClass>* getterCallback = new CallBackToMemberReturn<float, TClass>(owner, getter);
    // some unrelated processing
    [...]
}

编译器为CallBackToMemberSingleArg和的两个构造函数给了我以下错误CallBackToMemberReturn

No matching constructor for initialization of 'CallBackToMemberSingleArg<float, ValidationModule>'
No matching constructor for initialization of 'CallBackToMemberReturn<float, ValidationModule>'

这是构造函数声明

回调.h

template <typename TParam>
class CallBackSingleArg
{
public:
    virtual ~CallBackSingleArg() { }
    virtual void call(const TParam& p) = 0;
};

template <typename TParam, typename TClass>
class CallBackToMemberSingleArg : public CallBackSingleArg<TParam>
{
    void (TClass::*function)(const TParam&);
    TClass* object;
public:
    CallBackToMemberSingleArg(TClass* object, void(TClass::*function)(const TParam&)) : object(object), function(function) { };
    void call(const TParam& p) {
        (*object.*function)(p);
    }
};

template <typename TParam>
class CallBackReturn
{
public:
    virtual ~CallBackReturn() { }
    virtual TParam call() = 0;
};

template <typename TParam, typename TClass>
class CallBackToMemberReturn : public CallBackReturn<TParam>
{
    TParam (TClass::*function)();
    TClass* object;
public:
    CallBackToMemberReturn(TClass* object, TParam(TClass::*function)()) : object(object), function(function) { };
    TParam call() {
        return (*object.*function)();
    }
};

我试图将实际的 setter 更改为void setRatio(const float& value),因为CallBackToMemberSingleArg它需要一个 const 引用参数,但这并没有帮助,而且对构造函数的调用也CallBackToMemberReturn给出了上述错误,所以肯定还有其他东西,我看不到。

4

1 回答 1

0

您将 aTClass作为第一个参数传递给CallBackToMemberSingleArg构造函数,但 aTClass*是预期的。

于 2012-09-05T13:00:24.893 回答