tkn156 tkn156 - 1 year ago 63
C++ Question

c++ member functions with unresolved overload calling member functions

I'm trying to make an object with a member function that calls another member function like so

foo foo1 = new foo(1, 2);

I have class:

class foo
foo(int x, int y) {i = x; j = y;};
void const print(void const f());
void const printi();
void const printj();
int i;
int j;

and my implementation is something like:

void const foo::printi(){
std::cout << i;
void const foo::printj(){
std::cout << j;
void const foo::print(void const f()){

I'm getting an error of
[Error] no matching function for call to 'foo::print()'

Why is that, and how can i fix it?

Answer Source

You need to:

  1. Declare the pointer-to-member function parameter as such:
    void const print(void const (foo::*f)());
  1. Pass the member function pointer correctly:
  1. Call it with an actual instance (member function call requires an instance):
    void const foo::print(void const (foo::*f)()){

Alternatively you can make the instance an additional parameter or use std::bind or boost::bind to bind them together.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download