【发布时间】:2010-12-02 17:28:29
【问题描述】:
好的...首先,我将解释一下我所追求的架构是什么样的。
我正在尝试为我的 Stats 对象实现 Stats Manager。这个统计管理器是使用模板设计的(参见图片中的声明)。StatMgr 拥有一个映射(不是 STL),它将字符串实现映射到智能指针(也是一个模板),ARef。 StatMgr 的特定子类在构造时静态声明其父 StatMgr 的模板类(参见 LStatMgr 和 RStatMgr),因此它们本身不是模板类。
这使我可以执行以下操作:
LStatMgr myLStatMgr(10);
if(myLStatMgr.remove(acKey))
cout << "Remove was good" << endl;
//No need to delete :)
或
ARef<LStat> oLStat = NULL;
myLStatMgr.getNextStat(acKey,oLStat);
if(oLStat != NULL)
oLStat->doSomethingLStatLike();
然而那是在链接器先生和编译器女士决定联手阻止我的进步尝试之前(我仍然需要测试功能和内存使用!)。
就目前而言,每个统计类都有以下错误和警告:
.\StatsMgr.cpp(740):警告 C4661: 'void StatsMgr::vPrint()' : 不合适 为显式提供的定义 模板实例化请求
.\StatsMgr.cpp(740):警告 C4661: 'void StatsMgr::vPrint()' : 不合适 为显式提供的定义 模板实例化请求
.\StatsMgr.cpp(104):错误 C2084: 函数'StatsMgr::StatsMgr(const enum doCollect,const int)' 已经有 正文
.\StatsMgr.cpp(104) : 错误 C2084: 函数 'StatsMgr::StatsMgr(const enum doCollect,const int)' 已经有 身体
现在关于警告 :: 在第 740 行有声明:
template class StatsMgr<LStat>;
template class StatsMgr<RStat>;
我添加了这个,因为如果我不这样做,我最终会遇到一堆链接器错误(见下文)。但是,在所有 Stat 子类中都有 vPrint() 的声明和实现!
关于错误:基类“StatsMgr”具有定义的 COTR,子类 ( LStatMgr 和 RStatMgr)。为什么继承不能简单地解决这个问题?显然,这与 CPP 末尾的显式模板实例有关,但我无法理解到底发生了什么。
以下来自 StatsMgr.cpp
template<class type>
StatsMgr<type>::StatsMgr(const doCollect eOption, const int nListSize) :
oMyMap(wHashString, nListSize), oMyMapIter(oStatsList)
{
m_eCollectionOption = eOption;
}
以下来自 LStatMgr.cpp
LStatMgr::LStatMgr(const doCollect eOption, const int nListSize) :
StatsMgr<LStat> (eOption, nListSize)
{
}
我尝试替换
class LStatMgr;
class RStatMgr;
对于模板声明,这会传递编译错误,但链接器找不到任何未覆盖的函数。例如,如果我在 StatMgr 中有一个名为“getNextStat()”的函数并在 RStatMgr 中覆盖该函数,但在 LStatMgr 中没有,则链接器会对此抱怨。为什么继承没有涵盖这种情况?
这让我很难过。我可能不得不退回到作文(这应该是首选,但在这种情况下不是)。
顺便说一句...我正在使用 MSVC++ 4.1 (!!!) 进行编译,所以我理解您是否无法复制,但请帮忙。
谢谢,
丹尼斯。 (对不起,很长的帖子)
【问题讨论】:
-
回顾一下,我需要明确以下几点:StatMgr 是一个模板类:class StatMgr
{...} StatMgr 的子类不是... 例如:class LStatMgr:public StatMgr {...} -
如果您想添加更多信息,您应该可以编辑您的问题
-
@Steve - 干杯,也这样做了。 :)
标签: c++ visual-c++ templates inheritance linker