【问题标题】:C++ - Template function call operator overload - Error C2064C++ - 模板函数调用运算符重载 - 错误 C2064
【发布时间】:2012-09-20 20:59:20
【问题描述】:

我正在尝试在 c++ 中重载函数调用运算符,但我遇到了无法解决的编译错误 (Visual Studio 2010)。

错误在act(4);行中

#include <stdio.h>
#include <iostream>

void Test(int i);
template <class T> class Action
{
    private:
        void (*action)(T);
    public:
        Action(void (*action)(T))
        {
            this->action = action;
        }
        void Invoke(T arg)
        {
            this->action(arg);
        }
        void operator()(T arg)
        {
            this->action(arg);
        }
};

int main()
{
    Action<int> *act = new Action<int>(Test);
    act->Invoke(5);
    act(4);     //error C2064: term does not evaluate to a function taking 1 arguments overload
    char c;
    std::cin >> c;

    return 0;
}

void Test(int i)
{
    std::cout << i;
}

【问题讨论】:

  • 顺便说一句,#include &lt;stdio.h&gt; 在这里完全没用,因为你从不使用它,无论如何都会被&lt;iostream&gt; 取代。

标签: c++ templates operator-overloading function-call-operator


【解决方案1】:

act 仍然是一个指针,您必须先取消引用它,如下所示:

(*act)(4);

【讨论】:

  • 或者一开始就在栈上分配。
  • 或者那样,但我猜他在堆栈上尝试过,看到它工作,然后在堆上尝试并遇到编译错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多