【问题标题】:C++ error use of class template requires template argument listC++错误使用类模板需要模板参数列表
【发布时间】:2019-08-05 00:55:31
【问题描述】:

我正在尝试将函数作为参数传递给类,但出现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 What are template deduction guides and when should we use them? 传递函数时尝试使用自动模板推导功能

【问题讨论】:

  • 哪一行触发了错误?您使用的是哪个编译器、版本和标志?

标签: c++ class templates


【解决方案1】:

您只是缺少A::f2()定义中的模板参数:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多