-3

UPDATE I realize this question's lacking a proper MCVE, it will take me some time to come up with one. I will update it when I have time to come back to this, sorry. I appreciate the answers thus far.


Following this answer regarding static functions:

Declaration (in MyClass)

void MyClass::func ( void (MyOtherClass::*f)(int) ); //Use of undeclared identifier 'MyOtherClass'

Example of function being passed to func:

void MyOtherClass::print ( int x ) {
      printf("%d\n", x);
}

Function call (in MyOtherClass)

void MyOtherClass::loop(){
    func(&MyOtherClass::print);
}

How can one pass a member function as a parameter of a member function of another class?

A__
  • 1,616
  • 2
  • 19
  • 33
  • Use `std::function` as parameter and a lambda binding to the instance of `MyOtherClass`. – πάντα ῥεῖ Oct 09 '18 at 16:57
  • Are you asking why you are getting a "Use of undeclared identifier" error? – Drew Dormann Oct 09 '18 at 16:58
  • Unclear what the problem is, but some good, general purpose reading on Method Pointers: https://isocpp.org/wiki/faq/pointers-to-members – user4581301 Oct 09 '18 at 17:01
  • @DrewDormann Essentially yes! It's a syntax question. I'm trying to pass a member function as a parameter of another member function, but the member functions are in different namespaces, so I can't follow the syntax in various online tutorials/explanations. – A__ Oct 09 '18 at 17:14
  • @user4581301 Skimming through that was how I got where I am above. I didn't see any mention of how to deal with passing member functions between classes. Is using `typedef` somehow going to solve that? – A__ Oct 09 '18 at 17:21
  • @user4581301 Also meta: sorry that my problem is unclear! Please let me know what you think is vague, I'm not sure how to improve the question without sacrificing brevity as is. – A__ Oct 09 '18 at 17:24
  • 1
    Unclear because it's hard to tell what you're trying to do with it. You have most of what you are doing correct. Part of this is my fault, I didn't scroll the screen over far enough to see the error message in the comment. Once I did that, it's pretty clear that your problem is `MyOtherClass` has not been declared before `MyClass::func`. The compiler hasn't seen `MyOtherClass` yet and is unsure of how to interpret the unknown identifier. A [mcve] would make everything crystal clear. – user4581301 Oct 09 '18 at 17:29
  • Lacking an MCVE, my top suspicion as to the source of the actual problem is a missing header, probably because of a circular include. More on that here: [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – user4581301 Oct 09 '18 at 17:52

2 Answers2

0

According to the ISO, the answer is "don't". Unlike normal functions, a non-static member function is meaningless without an instance of the class. As a workaround, you can have your calling function take a std::function and pass it a lambda.

Example:

void calling_func(std::function<void()> f);

struct foo
{
    void func();

    void call()
    {
        calling_func([this]{
            func();
        });
    }
};
apophis42
  • 64
  • 4
0

can't you just use std::function and std::bind to that?

class MyOtherClass
{
public:
  MyOtherClass() {}
  void print(int x)
  {
    printf("%d\n", x);
  }
};


class MyClass
{
private:
  std::function<void()> CallbackFunc;

public:
  MyClass() {};
  void AssignFunction(std::function<void(int)> callback, int val)
  {
    CallbackFunc = std::bind(callback, val); //bind it again so that callback function gets the integer.
  }

  void DoCallback()
  {
    CallbackFunc(); //we can then just call the callback .this will, call myOtherClass::print(4)
  }
};

int main()
{
  MyClass myObject;
  MyOtherClass myOtherObject;
  int printval = 4;

  //assign the myObject.callbackfunc with the myOtherClass::print()
  myObject.AssignFunction(std::bind(&MyOtherClass::print, myOtherObject,std::placeholders::_1), 4);

  //calling the doCallback. which calls the assigned function.
  myObject.DoCallback();
  return 0;
}
Yucel_K
  • 688
  • 9
  • 17
  • 1
    They probably can, and it would be a better solution (though not quite as good a `std::function` and a lambda), but this is not the problem they have run into. The actual problem cannot be nailed down without a MCVE. – user4581301 Oct 09 '18 at 18:23