简体   繁体   中英

c++ Trouble casting overloaded function: <unresolved overloaded function type>

I have functions in a superclass designed to associate a string with internal functions:

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;
}

The idea being that any subclass I can register functions can call them with a string and value:

SubBase::SubBase() : Base(){
    Base::registerInput(
        "Height", 
        static_cast<void (*)(double)>(&SubBase::setHeight)
    );
}

void SubBase::setHeight(double h) {
....
}

Which could then be called with:

subBaseInstance.setInput("Height", 2.0);

However when I compile I'm getting the following error:

In constructor ‘SubBase::SubBase()’
error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(double)’

What am I missing?

As others noted, the types doesn't match. However you could use std::bind for it to work:

Base::registerInput(
    "Height", 
    std::bind(&SubBase::setHeight, *this, std::placeholders::_1)
);
typedef std::function<void(double)> double_v;

It's function-pointer .

static_cast<void (*)(double)>(&SubBase::setHeight)

It's member-function pointer . They are not convertible to each other.

You can use std::bind for this

SubBase::SubBase() : Base(){
    Base::registerInput(
        "Height", 
        std::bind(&SubBase::setHeight, this, std::placeholders::_1)
    );

SubBase is not static , so it has an implicit first argument of SubBase* (the object you call the member function for). Hence the signature is void (*) (SubBase*, double) . In C++11 you can probably (I'm not completely sure) cast it to a function<void (SubBase*, double)> .

Using lambda functions, you can do the following:

SubBase::SubBase() : Base(){
    auto myself = this;
    Base::registerInput( 
        "Height",  
        [myself] (double v) { myself->setHeight (v); }
    ); 
} 

void SubBase::setHeight(double h) { 
.... 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM