繁体   English   中英

C ++错误使用类模板需要模板参数列表

[英]C++ error use of class template requires template argument list

我试图将函数作为参数传递给类,但是由于use of class template requires template argument list而出现错误, use of class template requires template argument list

#include <iostream>

void f_global ()
{
    std::cout << "In function global\n";
}

template<typename F>
class A
{
public:
    F f1;
    A(const int var1, F fun1) : a(var1), f1(fun1) {}
    void f2();

private:
    int a;

};

void A::f2()
{
    std::cout << "in A::f2()\n";
}
int main()
{
    A obj(5,f_global);
    obj.f2();
}

我正在尝试从obj传递函数时使用自动模板推导功能。 什么是模板推导指南?何时应使用它们?

您只是在A::f2()的定义中缺少了template参数:

template<typename F>
void A<F>::f2()
{
    std::cout << "in A::f2()\n";
}

当成员函数定义在类定义之外时,则上述语法对于提供正确的模板实例化是必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM