C++ C++;:递归模板参数错误
我已经习惯于在Delphi/Lazarus中编写这样的代码:C++ C++;:递归模板参数错误,c++,templates,multiple-inheritance,C++,Templates,Multiple Inheritance,我已经习惯于在Delphi/Lazarus中编写这样的代码: type TPair<TKey, TValue> = record Key:TKey; Value:TValue; end; TAbstractList<T> = class end; TAbstractPairList<TKey, TValue> = class(TAbstractList<TPair<TKey,
type
TPair<TKey, TValue> = record
Key:TKey;
Value:TValue;
end;
TAbstractList<T> = class
end;
TAbstractPairList<TKey, TValue> = class(TAbstractList<TPair<TKey, TValue>>)
end;
类型
TPair=记录
键:TKey;
价值:TValue;
结束;
TAbstractList=class
结束;
TAbstractPairList=类(TAbstractList)
结束;
我想,C++中的模板更强大。但是
#include <stdio.h>
template <class TKey, class TValue>
struct TPair
{
TKey Key;
TValue Value;
};
template <class T>
class TAbstractList
{
public:
virtual void DoSomething() = 0;
};
template <class T>
class TList:public virtual TAbstractList<T>
{
public:
virtual void DoSomething() {
printf("%s", "Implementation!");
};
};
template <class TKey, class TValue>
class TAbstractPairList:public virtual TAbstractList<TPair<TKey, TValue>>
{
};
template <class TKey, class TValue>
class TPairList:public virtual TAbstractPairList<TKey, TValue>,
virtual TList<TPair<TKey, TValue>>
{
};
TAbstractPairList<int, int> *List;
int main()
{
List = new TPairList<int, int>;
List->DoSomething();
delete List;
};
#包括
模板
结构TPair
{
TKey键;
t价值;
};
模板
类选项卡列表
{
公众:
虚空DoSomething()=0;
};
模板
类TList:公共虚拟选项卡列表
{
公众:
虚空剂量测定法(){
printf(“%s”,“实现!”);
};
};
模板
类TAbstractPairList:公共虚拟TAbstractList
{
};
模板
TPairList类:公共虚拟选项卡TAbstractPairList,
虚拟列表
{
};
TAbstractPairList*列表;
int main()
{
列表=新的TPairList;
列表->剂量测量();
删除名单;
};
<>这个代码编译在在线C++编译器中,但不能在实际的编译中编译,比如Borland C++ +Builder。
也就是说,这一个有效:
typedef TPair<int, int> TMyPair;
TAbstractList<TMyPair> *List;
typedef TPair TMyPair;
选项卡列表*列表;
但这一条没有:
TAbstractList<TPair<int, int>> *List;
TAbstractList*列表;
我不能把“typedef”放在“模板”和“类”之间
这个代码有什么问题?为什么在某些C++编译器中递归模板参数是不可能的?这个问题能以某种方式解决吗?错误是什么?也许它太旧了,生锈了,以至于不能正确解析
>
?你应该尝试在中间添加一个额外的空间。如果你有一个真正的C++编译器,它可能会在模板的结尾看到“代码> >代码>作为一个操作符。“修复”是用来插入一个空间<代码> > >代码>“表达式语法”,接着是“不能从‘TBEY’生成模板特化”“哦,是的,它帮助了,thx。我知道,BC+++ 3.1已经过时了,但是我的目标是学习C++,因此测试不同环境下的不同程序。有人能告诉我,为什么这段代码在BC++3.1中编译,但却混淆了这段编译器?首先,它抱怨TPairList是抽象的,而事实并非如此。若我在TAbstractList-code compiles中声明了伪过程,则会执行List->DoSomething(),但看起来堆损坏很快就会发生。“空指针赋值”发生在程序终止时,测试表明TPairList析构函数从未执行过。