3

I have implemented some test functionals yesterday and everything compiled and worked fine without errors. Today i came back to my PC and my std::bind's are underlined red but compile without error. Seems like Intellisense and the compiler do not agree on the std::bind type. How can I fix this?

#include <functional>

class MyClass {
public:
    int doE() {
        return 0;
    }

    int doF() {
        return 1;
    }
};


void main()
{
    MyClass obj;
    std::function<int()> f = std::bind(&MyClass::doE, obj); // underlined red
    std::cout << f();
}

The error message is as follows:

Error (active)
    no suitable user-defined conversion from "std::_Binder<std::_Unforced, int (MyClass::*)(), MyClass &>" to "std::function<int ()>" exists
    functionals
    c:\functionals\functionals\thirdFunctionOnObject.h

I do have the same error type (Intellisense saying there is an error, but it compiles just fine) in more sophisticated code, where I used std::mem_fn().

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
coJetty
  • 47
  • 3

1 Answers1

2

Had the same problem with VS 2015 C++, I hate it. Microsoft drives me crazy.

For now I am using a nasty work around by moving the code to a static function. In your case it will be similar to the following:

class MyClass {
    // other code

    int doE () {
        return 0;
    }

    static int statDoE(MyClass * myClass) {
        return myClass->doE();
    }

}
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43