Error message:
...error C3867: 'temp::addition': function call missing argument list; use '&temp::addition' to create a pointer to member

#include "stdafx.h"
#include <iostream>

using namespace std;

struct temp
{
	int addition(int x, int y)
	{return (x + y);}

	int operation (int x, int y, int (*functocall)(int,int))
	{
		int g;
		g = (*functocall)(x,y);
		return (g);
	}
};


int main ()
{
  int m;
  
  temp momo;

  m = momo.operation(3, 4, momo.addition);  // !!!???
  cout << m;

  cin.get();
  return 0;
}

Dani AI

Generated

The error comes from treating a non‑static member function like a plain function pointer. Member functions have the special "pointer-to-member" type and must be called through an object (or made static). 's attempt to take the address of the object member caused the syntax error; 's suggestion to make addition static is the simplest fix if the function does not need this. pointed to the pointer-to-member reference — that topic explains the details.

A direct fix is to change operation to accept a pointer-to-member and call it on the object. Example:

struct temp {
    int addition(int x, int y) { return x + y; }

    int operation(int x, int y, int (temp::*fn)(int,int)) {
        return (this->*fn)(x, y); // call the member on this instance
    }
};

int m = temp().operation(3, 4, &temp::addition);

An alternative (more flexible) approach uses std::function and a binder or lambda so operation takes any callable:

#include <functional>

struct temp {
    int addition(int a, int b) { return a + b; }

    int operation(int x, int y, std::function<int(int,int)> f) {
        return f(x, y);
    }
};

temp momo;
using namespace std::placeholders;
int m = momo.operation(3, 4, std::bind(&temp::addition, &momo, _1, _2));
// or: int m = momo.operation(3,4, [&momo](int a,int b){ return momo.addition(a,b); });

Notes: make the member static only if it does not access instance data. If binding a member to an object, ensure that object outlives the callable. std::function and std::bind give convenience but add small overhead; prefer a pointer-to-member when you want zero indirection and you will call on the same instance.

Recommended Answers

All 6 Replies

m = momo.operation(3, 4, momo.addition);

Try to send a reference of that function .

m = momo.operation(3, 4, &momo.addition);

Try to send a reference of that function .

m = momo.operation(3, 4, &momo.addition);

syntax error, dude.

.

nice site. but reading thru all that would rather have me getting over this.

Easy way out. Make member func static :

#include <iostream>

using namespace std;

struct temp
{
	static int addition(int x, int y)
	{return (x + y);}

	int operation (int x, int y, int (*functocall)(int,int))
	{
		int g;
		g = (*functocall)(x,y);
		return (g);
	}
};


int main ()
{
  int m;
  
  temp momo;

  m = momo.operation(3, 4, temp::addition);  
  cout << m;

  cin.get();
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.