我有这些我们称之为“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/