Den*_*nis 5 c++ inheritance linker templates visual-c++
好的......首先,我只是要解释一下我所追求的架构是什么样的.

我正在尝试为我的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 :)
Run Code Online (Sandbox Code Playgroud)
要么
ARef<LStat> oLStat = NULL;
myLStatMgr.getNextStat(acKey,oLStat);
if(oLStat != NULL)
oLStat->doSomethingLStatLike();
Run Code Online (Sandbox Code Playgroud)
然而,那是在先生链接器和女士编译器决定联手并阻止我的进展尝试之前(我仍然需要测试功能和内存使用!).
目前我每个统计类都有以下错误和警告:
.\ 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行,有这样的delarations:
template class StatsMgr<LStat>;
template class StatsMgr<RStat>;
Run Code Online (Sandbox Code Playgroud)
我添加了这个,因为如果我不这样做,那么我最终会遇到一堆链接器错误(见下文).但是,在所有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;
}
Run Code Online (Sandbox Code Playgroud)
以下是LStatMgr.cpp
LStatMgr::LStatMgr(const doCollect eOption, const int nListSize) :
StatsMgr<LStat> (eOption, nListSize)
{
}
Run Code Online (Sandbox Code Playgroud)
我试过替换
class LStatMgr;
class RStatMgr;
Run Code Online (Sandbox Code Playgroud)
对于模板声明,这会传递编译错误,但链接器找不到任何未重写的函数.例如,如果我在StatMgr中有一个名为"getNextStat()"的函数并在RStatMgr中覆盖它而在LStatMgr中没有,那么链接器会抱怨这一点.为什么遗产不能涵盖这种情况?
这让我悲伤.我可能不得不回到构图(这应该是首选,但不是在这种情况下).
顺便说一句......我正在使用MSVC++ 4.1(!!!)编译,所以我理解你是否可以复制,但请帮忙.
谢谢,
丹尼斯.(抱歉很长的帖子)
| 归档时间: |
|
| 查看次数: |
3362 次 |
| 最近记录: |