术语不评估为采用1个参数的函数

pin*_*ngu 4 c++ function-pointers

有人可以解释我为什么得到:

错误C2064:术语不评估为带有1个参数的函数

对于该行:

DoSomething->*pt2Func("test");
Run Code Online (Sandbox Code Playgroud)

与这堂课

#ifndef DoSomething_H
#define DoSomething_H

#include <string>

class DoSomething
{
public:
    DoSomething(const std::string &path);
    virtual ~DoSomething();

    void DoSomething::bar(const std::string &bar) { bar_ = bar; }

private:
    std::string bar_;
};

#endif DoSomething_H
Run Code Online (Sandbox Code Playgroud)

#include "DoSomething.hpp"

namespace
{

void foo(void (DoSomething::*pt2Func)(const std::string&), doSomething *DoSomething)
{
    doSomething->*pt2Func("test");
}

}

DoSomething::DoSomething(const std::string &path)
{
    foo(&DoSomething::bar, this);

}
Run Code Online (Sandbox Code Playgroud)

And*_*owl 12

问题#1:第二个参数的名称和第二个参数的类型以某种方式交换.它应该是:

      DoSomething* doSomething
//    ^^^^^^^^^^^  ^^^^^^^^^^^
//    Type name    Argument name
Run Code Online (Sandbox Code Playgroud)

代替:

    doSomething* DoSomething
Run Code Online (Sandbox Code Playgroud)

这是你拥有的.

问题2:您需要添加几个括号才能正确解除引用功能:

    (doSomething->*pt2Func)("test");
//  ^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

最终,这是你得到的:

void foo(
    void (DoSomething::*pt2Func)(const std::string&), 
    DoSomething* doSomething
    )
{
    (doSomething->*pt2Func)("test");
}
Run Code Online (Sandbox Code Playgroud)

以下是您的程序编译的实例.