首页 > 解决方案 > 将函数作为参数传递时无效使用非静态成员函数

问题描述

谁能帮我修复这个错误“错误:非静态成员函数的无效使用”行 测试(3,a.foo); 当我编译代码时

#include <iostream>
#include <string>

class A {
 public:
  int x = 0;
  void foo(int y){
   x = x + y;    
  }
};
template<typename Func>
void test(int d, Func f){
  f(d);    
}

int main()
{
  A a;
  a.foo(4);
  std::cout << a.x;
  test(3, a.foo);
}

标签: c++functionnon-static

解决方案


推荐阅读