【问题标题】:term does not evaluate to a function taking 1 arguments术语不计算为带 1 个参数的函数
【发布时间】:2013-03-10 11:15:19
【问题描述】:

谁能解释我为什么会得到:

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

换行:

DoSomething->*pt2Func("test");

这个类

#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

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

}

【问题讨论】:

  • 你没有打错这一行:void foo(void (DoSomething::*pt2Func)(const std::string&amp;), doSomething *DoSomething)?它不应该包含DoSomething* doSomething吗?也就是最后2个字换了?

标签: c++ function-pointers


【解决方案1】:

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

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

代替:

    doSomething* DoSomething

你有什么。

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

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

最终,这就是你得到的:

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

这是您的程序编译的live example

【讨论】:

  • “现场示例”进入诈骗网站
猜你喜欢
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 2011-09-27
相关资源
最近更新 更多