[英]template class constructor with pointer to member function as parameter
I have following setup, which gives me headaches because I don't see the problem: 我有以下设置,这让我头疼,因为我看不到问题所在:
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
takes two pointers to member functions of ValidationModule
registerFloat
两个指向ValidationModule
成员函数的指针
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; }
Settings.h 设置.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
[...]
}
The compiler gives me following errors for the two constructors of CallBackToMemberSingleArg
and CallBackToMemberReturn
: 对于
CallBackToMemberSingleArg
和CallBackToMemberReturn
的两个构造函数, 编译器给我以下错误 :
No matching constructor for initialization of 'CallBackToMemberSingleArg<float, ValidationModule>'
No matching constructor for initialization of 'CallBackToMemberReturn<float, ValidationModule>'
Here are the constructor declarations 这是构造函数声明
Callback.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)();
}
};
I tried to change the actual setter to void setRatio(const float& value)
, because CallBackToMemberSingleArg
takes a const reference parameter, but that didn't help and also the call to the constructor of CallBackToMemberReturn
gives the above error, so there must be something else, which I am unable to see. 我试图将实际的setter更改为
void setRatio(const float& value)
,因为CallBackToMemberSingleArg
采用const引用参数,但这无济于事,而且对CallBackToMemberReturn
构造函数的调用也出现上述错误,因此还必须有其他内容,我看不到。
您正在将TClass
作为第一个参数传递给CallBackToMemberSingleArg
构造函数,但是应该使用TClass*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.