c++ - 错误 : Reference to non-static member function must be called (2)

标签 c++

我有一段这样的代码

class A {
public:

typedef int (A::*AFn)(int);
std::map<std::string, AFn> fm_;

A() {
    fm_.insert("fn1", fn);
}

int fn(int a) {
    return a;
}

};

我得到一个编译时错误说 error: reference to non-static member function must be called fm_.insert("fn1", fn);

为什么会发生这种情况,我该如何纠正?

最佳答案

因为 fn 是一个非静态成员函数,一个单独的 fn 不是一个有效的表达式。在此上下文中,您可以对非限定 fn 做的唯一事情是调用它:fn(something)。这就是编译器告诉您的内容。

如果你想获得一个指向成员函数A::fn的指针,你必须明确地使用运算符&并提供一个合格的成员名称:&A::fn.

关于c++ - 错误 : Reference to non-static member function must be called (2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153875/

相关文章:

C++ - 使用 Bag of Words 将图片匹配在一起?

c++ - 如何从cpprestsdk解析json数据

c++ - 在 C++ 中将信息写入文件的函数

c++ - 如何手动创建将在析构函数中阻塞的 future

c++ - 读取 argv 时 XCode BAD ACCESS

c++ - 如何使用 Qt c++ QGeoServiceProvider 从地理地址获取纬度/经度?

c++ - 错误 : non-static reference member 'std::ostream& Student::out' , 无法使用默认赋值运算符

c++ - 如何降低 OpenCV 中图像的分辨率?

C++17 如何保存通用可调用对象以供以后使用

c++ - NEON:将 int8x16_t 解包为一对 int16x8 并将一对 int16x8_t 打包为 int8x16_t