如何实现模板类中的嵌套模板类

pampa
业界专家认证
2003-02-11 04:50:31
我想在一个模板类中实现一个嵌套类,这个嵌套类也是模板类.
代码如下:
template<class TYPE>
class CTemplate
{
public:
struct CNest
{
friend class CTemplate<TYPE>;
private:
CNest(){}
void DoNothing() {}
};

public:
CTemplate();
};

template<class TYPE>
inline CTemplate<TYPE>::CTemplate()
{
CNest nest;
nest.DoNothing();
}

在vc6中可以编译通过,并且运行正常,
但在 Solaris上 SUN自带的CC编译器 却编译不过
请问上述例子程序是否存在语法问题?
VC6 和CC 不一致, 是 VC6的问题,还是CC的问题?
如何写出两个编译器都能通过的 "模板类中的嵌套模板类"?

哪位大虾知道, 请指点!
本来想给200分, 谁知bbs不让给这么多, 先给100; 如果OK了,我再发一个帖子给100
谢谢!
...全文
388 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
EXTH 2003-02-14
  • 打赏
  • 举报
回复
vc6===hp stl
gcc===sgi stl
这二个stl的实现又很多差异,可看看源代码。
hellwolf 2003-02-13
  • 打赏
  • 举报
回复
程序是对的,是哪个什么编译器的问题(可能太老了)
allen1981813 2003-02-12
  • 打赏
  • 举报
回复
// See See What I have done ,maybe gave you some reference...
// Gohead togethere....
#include<iostream>
#include<typeinfo.h>
using namespace std;

#ifndef LIST_H_
#define LIST_H_

template<class>class ListIterator;//Maybe your problem...

template<class T>
class Node{
T i_value;
Node<T>* i_next;
friend class ListIterator;
public:
Node(const T& anObj):i_value(anObj),i_next(0){}
Node(const Node<T>& anObj):i_value(anObj.i_value),i_next(anObj.i_next){}

Node<T>* next()const{return i_next;}
T value()const{return i_value;}
void connect(Node<T>* const anObj){i_next=anObj;}
};

template<class T>
class List{
Node<T>* i_node;
Node<T>* i_head;
int i_size;

void copyList(List<T>&);
public:
List():i_node(0),i_head(i_node),i_size(0){}
List(List<T>&);

void add(Node<T>&);
void creat(const T&);
void deltree();
void del(int);

void backward();
void rewind();
Node<T>& operator--(int);
Node<T>& operator--();
const List<T>& operator+=(List<T>&);
List<T>& operator=(List<T>& right);

T current()const;
T at(int);
T operator[](int offset);
friend ostream& operator<<(ostream os,List<T>& right);

int capacity()const;
};

//Iterator of List<T> defined...
template<class T>
class ListIterator{
List<T>& i_list;
public:
ListIterator(List<T>& aList):i_list(aList){}
//Pointer motive...
void rewind(){i_list.rewind();}
Node<T>* operator--(int)const{return &i_list.operator--(0);}
Node<T>* operator--()const{return &i_list.operator--();}
//Value access...
const T& operator*()const{return i_list.current();}
};

///////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
List<T>::List(List<T>& alist)
:i_node(0),
i_head(0),
i_size(0){copyList(alist);}

template<class T>
void List<T>::copyList(List<T>& alist){
for(int i=0; i<alist.i_size; i++)
creat(alist[i]);
}

//Tools one a List<T>...
template<class T>
inline void List<T>::add(Node<T>& aNode){ //Add new node of List<T> from outside
aNode.connect(i_head);
i_head=&aNode;
i_size++;
}

template<class T>
void List<T>::creat(const T& aValue){ //Creat a value directly in List<T>
Node<T>* temp=new Node<T>(aValue);
temp->connect(i_head);
i_head=temp;
i_size++;
}

template<class T>
void List<T>::deltree(){ //Del all Node<T>s
Node<T>* temp; //A temp to connect Node<T> want to del
while(i_head){
temp=i_head;
i_head=i_head->next();
delete temp;
}
i_head=0;
i_node=0;
i_size=0;
}

template<class T>
void List<T>::del(int offset){
rewind();
if(!i_head){
cout<<"Empty list here..."<<endl;
return;
}
else if(i_size-offset-1==0){
Node<T>* temp=i_head;
i_head=i_head->next();
delete temp;
i_size--;
return;
}
else if(!i_head->next()){
delete i_head;
delete i_node;
i_head=i_node=0;
i_size=0;
return;
}
else{
for(int i=0; i<i_size-offset-2; i++)
i_node=i_node->next();
Node<T>* temp=i_node->next();
i_node->connect(temp-> next());
delete temp;
rewind();
i_size--;
}
}

//Pointer motive...
template<class T>
inline void List<T>::backward(){
i_node=i_node->next();
if(!i_node) i_node=i_head;
}

template<class T>
inline void List<T>::rewind(){i_node=i_head;} //Can help to go to head of List<T>

template<class T>
Node<T>& List<T>::operator--(int){
Node<T>& temp=*i_node;
backward();
return temp;
}

template<class T>
Node<T>& List<T>::operator--(){
operator--(0);
return *i_node;
}

//Nodes value's access which is happen on own position...
//Value access...
template<class T>
inline T List<T>::current()const{return (i_node)?i_node->value():0;}

template<class T>
T List<T>::at(int offset){
rewind(); //Go to head of List<T>
for(int i=0; i<i_size-offset-1; i++)
i_node=i_node->next();
return i_node->value();
}

template<class T>
T List<T>::operator[](int offset){return at(offset);}

template<class T>
ostream& operator<<(ostream os,List<T>& right){ //Display all Node<T>'s value
right.rewind();
if(!right.capacity())
cout<<"Empty list ..."<<endl;
else{
for(int i=right.capacity(); i>0; i--)
os<<"Element["<<i-1<<"]="<<right[i-1]<<endl;
os<<"List end..."<<endl;
}
return os;
}


//List<T> appendence...
template<class T>
const List<T>& List<T>::operator+=(List<T>& right){
for(rewind(); i_node->next(); i_node=i_node->next());
for(int i=0; i<right.i_size; i++){
i_node->connect(new Node<T>(right[right.i_size-i-1]) );
i_node=i_node->next();
i_size++;
}
rewind();
return *this;
}

//List<T> copy
template<class T>
List<T>& List<T>::operator=(List<T>& right){
if(this==&right) return *this;
deltree();
copyList(right);
rewind();
return *this;
}
template<class T>
inline int List<T>::capacity()const{return i_size;}

#endif


pampa 2003-02-12
  • 打赏
  • 举报
回复
谢谢 allen1981813() 网友
您大概都猜出来我要干什么事情了
我就是要作一个List类, 对外接口和MFC的CList基本一样,
内部采用STL的list实现
所以需要包装出一个POSITION来(使得对List的用户而言,感觉不到迭代器的存在),POSITION的对外和MFC::CList基本一样,内部采用stl::list的迭代器来实现.
所以需要 模板类中嵌套模板类.

不过经过我后来试验发现,错误不在 vc6和gcc编译器上, gcc也可以顺利编译通过上述代码,
问题在于 我使用了sgi stl中list的特定实现, gcc自带的stl版本的实现不完全一致, 所以就不能通过编译了,
还是要谢谢 allen1981813() 网友和KennyYuan(我是白菜) 网友
我再发一个帖子, 请两位去拿分

顺便问一下 KennyYuan(我是白菜)网友 , ISO14882 何处有下载
谢谢
oustar 2003-02-12
  • 打赏
  • 举报
回复
c++是通用的,但编译器对其的支持就大不相同了。
这也就是大部分源码开放的项目多用c编写的原因。
pampa 2003-02-12
  • 打赏
  • 举报
回复
把模板类重的嵌套模板类的函数写在类定义里面 可以按照下面的代码来实现了
但有谁知道 可否把嵌套类(CNest)的函数的实现部分写在类定义的外面, 就如同CTemplate的构造函数那样 写在类定义的外面?
如果可以,怎么写?

十分感谢!

例子代码:
template<class TYPE>
class CTemplate
{
public:
struct CNest
{
friend class CTemplate<TYPE>;
private:
CNest(){}
void DoNothing() {}
};

public:
CTemplate();
};

template<class TYPE>
inline CTemplate<TYPE>::CTemplate()
{
CNest nest;
nest.DoNothing();
}
KennyYuan 2003-02-11
  • 打赏
  • 举报
回复
凡此类问题,去看ISO14882

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧