首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

类模板有关问题 ver2

2012-02-28 
类模板问题 ver2template inti1,inti2classOne{template inti1,inti2friendostream&operator (ostr

类模板问题 ver2
template <int   i1,int   i2>
class   One
{
template <int   i1,int   i2>   friend   ostream&   operator < <(ostream&,const   One <i1,i2> &);
template <int   i1,int   i2>   friend   void   fun2(const   One <i1,i2> &);
};

template <int   i1,int   i2>   ostream&   operator < <(ostream&,const   One <i1,i2> &){}
template <int   i1,int   i2>   void   fun2(const   One <i1,i2> &){}

template <class   T>
class   Two
{
template <class   T>   friend   ostream&   operator < <(ostream&,const   Two <T> &);
template <class   T>   friend   void   fun1(const   Two <T> &);

};

template <class   T>   ostream&   operator < <(ostream&,const   Two <T> &){}
template <class   T>   void   fun1(const   Two <T> &){}

除了   fun1   其它3个函数都有2义性  

怎么解决?

[解决办法]
在class One的两个友元函数中,参数表的One <i1,i2> 的t1和t2是用的外层class One的模板参数,
还是友元声明中的参数列表??????另外你声明友元的时候,因为没有提前的声明,造成编译器无法
知道你放在那里的友元是什么东西!!!

你要的可能是(class Two我就不帮你改了,类似的):
//先是三个提前的声明(forward declaration)
template <int i1,int i2>
class One;
template <int i1,int i2>
ostream& operator < <(ostream&,const One <i1,i2> &);
template <int i1,int i2>
void fun2(const One <i1,i2> &);


template <int i1,int i2>
class One
{
friend ostream& operator < < <i1, i2> (ostream&, const One <i1,i2> &);
friend void fun2 <i1, i2> (const One <i1,i2> &);
};

template <int i1,int i2>
ostream& operator < <(ostream&,const One <i1,i2> &)
{
}


template <int i1,int i2>
void fun2(const One <i1,i2> &)
{
}
[解决办法]

template <int i1,int i2>
class One
{
template <int ii1,int ii2> friend ostream& operator < <(ostream&,const One <ii1,ii2> &);
template <int ii1,int ii2> friend void fun2(const One <ii1,ii2> &);
};

template <int ii1,int ii2> ostream& operator < <(ostream&x,const One <ii1,ii2> &){return x;}
template <int ii1,int ii2> void fun2(const One <ii1,ii2> &){}

template <class T>
class Two
{
template <class TT> friend ostream& operator < <(ostream&,const Two <TT> &);
template <class TT> friend void fun1(const Two <TT> &);

};

template <class TT> ostream& operator < <(ostream&x,const Two <TT> &){return x;}
template <class TT> void fun1(const Two <TT> &){}

热点排行