我在超类中有函数,旨在将字符串与内部函数相关联:
class Base
{
typedef std::function<void(double)> double_v;
bool registerInput(std::string const& key, double_v const& input) {
functions[key] = input;
}
void setInput(std::string key, double value) {
auto fit = functions.find(key);
if (fit == functions.end()) return;
fit->second(value);
}
std::map<std::string, double_v> functions;
}
这个想法是我可以注册函数的任何子类都可以用字符串和值调用它们:
SubBase::SubBase() : Base(){
Base::registerInput(
"Height",
static_cast<void (*)(double)>(&SubBase::setHeight)
);
}
void SubBase::setHeight(double h) {
....
}
然后可以调用:
subBaseInstance.setInput("Height", 2.0);
但是,当我编译时,出现以下错误:
In constructor ‘SubBase::SubBase()’
error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(double)’
我错过了什么?