C++ term 不计算为采用 1 个参数的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15321596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:15:56  来源:igfitidea点击:

term does not evaluate to a function taking 1 arguments

c++function-pointers

提问by pingu

Can someone explain why I am getting:

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

error C2064: term does not evaluate to a function taking 1 arguments

错误 C2064:术语不计算为采用 1 个参数的函数

for the line:

对于该行:

DoSomething->*pt2Func("test");

with this class

与这堂课

#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

and

#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);

}

回答by Andy Prowl

Problem #1:The name of the second argument and the type of the second argument are swapped somehow. It should be:

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

      DoSomething* doSomething
//    ^^^^^^^^^^^  ^^^^^^^^^^^
//    Type name    Argument name

Instead of:

代替:

    doSomething* DoSomething

Which is what you have.

这就是你所拥有的。

Problem #2:You need to add a couple of parentheses to get the function correctly dereferenced:

问题#2:您需要添加几个括号才能正确取消引用该函数:

    (doSomething->*pt2Func)("test");
//  ^^^^^^^^^^^^^^^^^^^^^^^

Eventually, this is what you get:

最终,这就是你得到的:

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

And here is a live exampleof your program compiling.

这是你的程序编译的一个活生生的例子