c++ - "Unable to resolve template based identifier X"- 从模板类调用模板参数类的方法 (C++)

标签 c++ templates subclass

我有这些我们称之为“MyGroup”的对象。 MyGroup子类 BaseGroup .作为一个实时系统,我们没有动态内存分配的奢侈,所以我们有一个 ObjectPool分配给定类型的一定数量的对象。所以在这种情况下我说 10 MyGroup分配的对象。

现在 - 我想将这些组存储在键值映射中。所以我创建了一个 FiniteMap模板类,用于创建给定大小的通用 map ,可以存储给定类型的对象。

我的 GroupList类子类 FiniteMap , 仍然允许参数 <G, S> , 因为我不知道 BaseGroup 的哪个子类我想存储在我的 GroupList 中。

BaseGroup::createGroup()方法是纯虚拟的,因此在 MyGroup 中实现.

所以 - 问题是,在标有 // *** 的行中,我在编译中得到以下两个之一:警告:Unable to resolve template based identifier createGroup ,或者如果我真的调用 getOrCreateGroup()方法,错误:cannot call member function 'virtual MyGroup* MyGroup::createGroup(unsigned int) without object .

在主逻辑中实例化GroupList对象:

GroupList<MyGroup, 10> myGroupList;

有什么想法吗?

我有这些模板类:

template <class T, unsigned int S>
class FiniteMap
{
    // class to implement an array of these types of structures.
    // Contains methods for put, remove, get, etc.
    struct Entry
    {
        int key;
        T* pObject;
        bool inUse;
        Entry() : key(0), pObject(NULL), inUse(false) {}
    }
}

template <class G, unsigned int S>
class GroupList : public FiniteMap<G, S>
{
    // ...
    G* getOrCreateGroup(unsigned int groupNumber)
    {
       G* pGroup = this->get(groupNumber);
       if (pGroup == NULL)
       {
          pGroup = G::createGroup(groupNumber);  // ***
          if (pGroup == NULL)
          {
             // handle error
          }
       }
       return pGroup;
    }
}

class BaseGroup
{
public:
    virtual BaseGroup* createGroup(unsigned int groupNumber) = 0;

protected:
    unsigned int _groupNumber;
}

class MyGroup : BaseGroup
{
public:
    virtual MyGroup* createGroup(unsigned int groupNumber)
    {
        MyGroup* g = MyGroupPool.New();   // Object pool allocation
        if (g == NULL)
        {
            // handle error
        }
        else
        {
            g->_groupNumber = groupNumber;
        }
        return g;
    }
}

最佳答案

你不能打电话

G::createGroup();

因为 createGroup 不是静态的,您也不是从 G 继承的。

您的意思是让 createGroup 静态化吗? (也是非虚拟的)我不清楚为什么这是虚拟的。

关于c++ - "Unable to resolve template based identifier X"- 从模板类调用模板参数类的方法 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36508571/

相关文章:

c++ - 比较 argv 和 L"test"不工作

c++ - 强制 automake 和 autoconf 在编译行后面设置 -lz 标志,并且在不更改 .ac 文件的情况下执行

c++ - 使对象恰好位于屏幕中央

c++ - 围绕显式模板实例化的困惑

c++ - "New"实现与 vtk 和子类

swift - 许多子类的一个 XIB View

Javascript 子类化和 createElement

c++ - WinINet异步模式灾难

c++ - 将 typedef 移动到函数模板参数时编译错误

c++ - 函数指针的函数模板特化