This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C2000 v6.4.9 Compiler Error: a pointer to a bound function may only be used to call the function

Guru 19975 points

Hello,

I am getting the above error in the attached project.  

AssignMemberFunctionToFunctionPointer.zip

The purpose of the program is to assign the address of a class's member function to a function pointer.

Why am I getting the error and how can I correct it?

Thanks,

Stephen

  • Ok, I found the fix to the issue at .

    The member function needs to be static and the resolution operator needs to be used, i.e TestStruct1.functionPtr = &TestClass::TestFunction;

  • Hello,

    I have another issue since I made the member function a static function.  Now, the static member function can't access the member variables.

    Does anyone have a way to solve this issue?

    Thanks,
    Stephen

  • I fixed the issue by having the static member function call  another member function in the same class.

    i.e.

    void TestClass::TestFunction(void) 
    {
    TestClass1.TestFunction2();
    }

    Is there a better way of doing it?

  • I'm really not sure what's going on with your test case; you haven't shown us the updated test case.
    A static member function gets called without an object, so it can't refer to any member variables unless those variables are also static.
  • Here's the updated project:

    AssignMemberFunctionToFunctionPointer2.zip

    "A static member function gets called without an object, so it can't refer to any member variables unless those variables are also static."

    That's correct. Is the solution shown in the attached project the best way to deal with this issue?

  • As you have noticed, a class's static member functions don't have access to non-static members.  Static member functions don't have the implicit this pointer which tells them which object (instance of the class) they are working on.

    If you always are going to be working with a known, static object, then your solution works fine.

    If you want to be able to work with different objects, then we might need to consider other options.

    One alternative is to change the signature to explicitly state which object they should work on:

        static void TestFunction(void);

    becomes

        static void TestFunctionStatic(TestClass & object);

    Here "object" takes the role of the implicit this pointer that a non-static member function has.

    Other options include using pointer-to-member-function syntax, or leverage C++'s <functional>.

    e.g.

    #ifndef TESTCLASS_H_
    #define TESTCLASS_H_
    
    class TestClass
    {
      public:
        TestClass();
        void TestFunction(void);
        static void TestFunctionStatic(TestClass & object);
      private:
        int myX;
    };
    
    extern TestClass TestClass1;
    
    #endif /* TESTCLASS_H_ */

    #include "TestClass.h"
    #include <functional>
    
    typedef void (*TestClassStaticFuncPtr)(TestClass &);
    typedef void (TestClass::*TestClassNonStaticFuncPtr)(void);
    typedef std::mem_fun_t<void, TestClass> AlternateTestClassNonStaticFunc;
    
    typedef struct TestStruct
    {
        TestStruct() :
          ptr1(NULL),
          ptr2(NULL),
          func3(NULL)
        {
        }
    
        TestClassStaticFuncPtr ptr1;
        TestClassNonStaticFuncPtr ptr2;
        AlternateTestClassNonStaticFunc func3;
    } TEST_STRUCT;
    
    TEST_STRUCT TestStruct1;
    
    int main(void)
    {
        TestStruct1.ptr1 = &TestClass::TestFunctionStatic;
        TestStruct1.ptr1(TestClass1);
    
        TestStruct1.ptr2 = &TestClass::TestFunction;
        (TestClass1.*(TestStruct1.ptr2))();
    
        TestStruct1.func3 = std::mem_fun(&TestClass::TestFunction);
        TestStruct1.func3(&TestClass1);
    
        return 0;
    }

  • I'll try that out and get back with you.

    Thanks,
    Stephen

  • Fantastic!!