c++ - 错误 : 'T' is not a template

标签 c++ templates c++14

我有一个用例的以下函数:

template<size_t state_dim, size_t action_dim>
class agent {
    // [...]
    /**
     * @brief get_plugin Get a pluging by name
     */
    template<typename T>
    inline T<state_dim, action_dim>* get_plugin() const {
        const string plugin = T<state_dim, action_dim>().name();
        for(size_t i = 0; i < this->_plugins.size(); i++)
            if(this->_plugins[i].first == plugin)
                return static_cast<T<state_dim, action_dim>*>(this->_plugins.at(i).second);
        return nullptr;
    }
    // [...]
}

// a possible usecase
auto sepp = instance.get_plugin<plugin_SEP>();

但是我得到以下错误:

error: 'T' is not a template
    inline T<state_dim, action_dim>* get_plugin(const string& plugin) const {
           ^

error: 'T' is not a template
    return static_cast<T<state_dim, action_dim>*>(this->_plugins.at(i).second);
                       ^

error: missing template arguments before '>' token
    auto sepp = instance.get_plugin<plugin_SEP>();
                                              ^

error: expected primary-expression before ')' token
    auto sepp = instance.get_plugin<plugin_SEP>();
                                                ^

我在这里错过了什么?

最佳答案

1.你需要声明T是一个template template parameter ,否则你不能使用(实例化)它与模板参数。

template <template <size_t, size_t> class T>
inline T<state_dim, action_dim>* get_plugin(const string& plugin) const {

2.调用成员函数模板get_plugin需要插入关键字template

auto sepp = instance.template get_plugin<plugin_SEP>();

参见 Where and why do I have to put the “template” and “typename” keywords?了解更多详情。

关于c++ - 错误 : 'T' is not a template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40277416/

相关文章:

android - android studio NDK 上的 turbo c++ graphics.h 程序

c++ - 为结构内部的指针 vector 分配内存

Spring Freemarker配置,未找到模板

c++ - 从元组中获取元素

c++ - 将两个 char* 带入另一个 std::string 的字符串构造函数在 c++14 中有效,但在 c++17 中无效

c++ - 在移动的 lambda 中捕获和调试对局部变量引用的无效使用

c++ - 使用 protobuf 的段错误

php - 全局访问模板中的用户对象

c++ - 非模板类中的可变参数成员

c++ - Boost Binary Endian 解析器不工作?