帮忙看个代码,类模板

selooloo 2010-02-25 03:40:30
这是我定义的类

#include <iostream>
using namespace std;

namespace Listsavitch
{
template<class ItemType>
class List
{
public:
List(int max);
~List();
int length()const;
void add(ItemType new_item);
bool full()const;
void erase();
friend ostream& operator <<(ostream& outs,const List<ItemType>& the_list);//这里出错
private:
ItemType *item;
int max_length;
int current_length;
};
}


函数的实现

template<class ItemType>
ostream& operator <<(ostream& outs,const List<ItemType>& the_list)
{
for(int i=0;i<the_list.current_length;i++)
outs<<the_list.item[i]<<endl;
return outs;
}


老提示错误[Warning] friend declaration `std::ostream&......

实在看不出哪里错误,各位帮帮忙啊
...全文
195 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2010-02-25
  • 打赏
  • 举报
回复
为啥我说“规定”的时候人家喜欢死活不信,akirya说的人家就多肯信呢。
人和人不能比啊。
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 22 楼 akirya 的回复:
引用 21 楼 selooloo 的回复:谢谢哈 ostream&operator < < <> 为什么要声明两次呢? <>这个是做什么的?
这是语法要求这么写的

再次感谢,分不够多,享与各位
  • 打赏
  • 举报
回复
引用 21 楼 selooloo 的回复:
谢谢哈
ostream&operator < < <> 为什么要声明两次呢? <>这个是做什么的?

这是语法要求这么写的
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 18 楼 akirya 的回复:
引用 15 楼 selooloo 的回复:引用 9 楼 akirya 的回复: 引用 8 楼 selooloo 的回复:引用 2 楼 akirya 的回复: 你用的VC6 ? VS2008直接编译通过. 用的DEV,按理代码都是书上的,应该不会错书上有错很正常我试了一下,也没问题。 估计就是编译器的问题吧 ,[Warning] friend declaration `std::ostream& 错误就提示到这,后面没显示了 囧
只是警告嘛
模板友元的标准写法是这样的
C/C++ code
#include<iostream>usingnamespace std;

template<class T>class test;

template<class T>
ostream&operator<<( ostream& os,const test<T>& src );

template<class T>class test
{
T m;public:
test( T i ): m( i ) {}
friend ostream&operator<<<> ( ostream& os,const test<T>& src );
};

template<class T>
ostream&operator<< ( ostream& os,const test<T>& src )
{
os<<"operator<<"<< src.m;return os;
}int main()
{
test<int> x(10 );
cout<< x<< endl;return0;
}


谢谢哈
ostream&operator<<<> 为什么要声明两次呢?<>这个是做什么的?
taodm 2010-02-25
  • 打赏
  • 举报
回复
呃,google “be friending templates”
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 17 楼 baihacker 的回复:
引用 16 楼 selooloo 的回复:引用 13 楼 baihacker 的回复:引用 12 楼 selooloo 的回复:引用 5 楼 baihacker 的回复:C/C++ codetemplate<typename IType> friend ostream&operator<< (ostream& outs,const List<IType>& the_list);//这里出错//如果是非绑定友元,要这样 多谢,这么改是好了,不过怎么访问不了私有变量current_length和item呢?换绑定的。 挺神奇啊,可以了,为什么要把ItemType换成IType啊,能解释下不?谢谢了
3楼给的是绑定的,也就是说每个List搞出来的模板类,绑定了一个对应的operator < <。
5楼的给的是非绑定的,也就是说每个模板类,对应了多个operator < <


还是非绑定的可行,虽然仍不太懂,还是谢谢了,结贴
  • 打赏
  • 举报
回复
引用 15 楼 selooloo 的回复:
引用 9 楼 akirya 的回复:
引用 8 楼 selooloo 的回复:引用 2 楼 akirya 的回复: 你用的VC6 ? VS2008直接编译通过. 用的DEV,按理代码都是书上的,应该不会错
书上有错很正常
我试了一下,也没问题。

估计就是编译器的问题吧
,[Warning] friend declaration `std::ostream&
错误就提示到这,后面没显示了 囧

只是警告嘛
模板友元的标准写法是这样的

#include<iostream>
using namespace std;

template<class T>
class test;

template<class T>
ostream& operator<<( ostream& os, const test<T>& src );

template<class T>
class test
{
T m;
public:
test( T i ): m( i ) {}
friend ostream& operator<< < > ( ostream& os, const test<T>& src );
};

template<class T>
ostream& operator<< ( ostream& os, const test<T>& src )
{
os << "operator<<" << src.m;
return os;
}

int main()
{
test<int> x( 10 );
cout << x << endl;
return 0;
}
baihacker 2010-02-25
  • 打赏
  • 举报
回复
引用 16 楼 selooloo 的回复:
引用 13 楼 baihacker 的回复:引用 12 楼 selooloo 的回复:引用 5 楼 baihacker 的回复:C/C++ codetemplate<typename IType> friend ostream&operator<< (ostream& outs,const List<IType>& the_list);//这里出错//如果是非绑定友元,要这样 多谢,这么改是好了,不过怎么访问不了私有变量current_length和item呢?换绑定的。

挺神奇啊,可以了,为什么要把ItemType换成IType啊,能解释下不?谢谢了

3楼给的是绑定的,也就是说每个List搞出来的模板类,绑定了一个对应的operator <<。
5楼的给的是非绑定的,也就是说每个模板类,对应了多个operator <<
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 13 楼 baihacker 的回复:
引用 12 楼 selooloo 的回复:引用 5 楼 baihacker 的回复:C/C++ codetemplate<typename IType> friend ostream&operator<< (ostream& outs,const List<IType>& the_list);//这里出错//如果是非绑定友元,要这样 多谢,这么改是好了,不过怎么访问不了私有变量current_length和item呢?
换绑定的。


挺神奇啊,可以了,为什么要把ItemType换成IType啊,能解释下不?谢谢了
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 9 楼 akirya 的回复:
引用 8 楼 selooloo 的回复:引用 2 楼 akirya 的回复: 你用的VC6 ? VS2008直接编译通过. 用的DEV,按理代码都是书上的,应该不会错
书上有错很正常
我试了一下,也没问题。


估计就是编译器的问题吧
,[Warning] friend declaration `std::ostream&
错误就提示到这,后面没显示了 囧
InfidelX 2010-02-25
  • 打赏
  • 举报
回复
vs2005也没有什么问题啊~
baihacker 2010-02-25
  • 打赏
  • 举报
回复
引用 12 楼 selooloo 的回复:
引用 5 楼 baihacker 的回复:C/C++ codetemplate<typename IType> friend ostream&operator<< (ostream& outs,const List<IType>& the_list);//这里出错//如果是非绑定友元,要这样

多谢,这么改是好了,不过怎么访问不了私有变量current_length和item呢?

换绑定的。
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 5 楼 baihacker 的回复:
C/C++ codetemplate<typename IType>
friend ostream&operator<< (ostream& outs,const List<IType>& the_list);//这里出错//如果是非绑定友元,要这样


多谢,这么改是好了,不过怎么访问不了私有变量current_length和item呢?
magic7004 2010-02-25
  • 打赏
  • 举报
回复
错了,operator<<必须在namespace Listsavitch里才行
  • 打赏
  • 举报
回复
lz贴一下报错信息吧
  • 打赏
  • 举报
回复
引用 8 楼 selooloo 的回复:
引用 2 楼 akirya 的回复:
你用的VC6 ?
VS2008直接编译通过.
用的DEV,按理代码都是书上的,应该不会错

书上有错很正常
我试了一下,也没问题。
selooloo 2010-02-25
  • 打赏
  • 举报
回复
引用 2 楼 akirya 的回复:
你用的VC6 ?
VS2008直接编译通过.

用的DEV,按理代码都是书上的,应该不会错
stardust20 2010-02-25
  • 打赏
  • 举报
回复
直接把定义写在类中也可以。。。
magic7004 2010-02-25
  • 打赏
  • 举报
回复
我编译是OK的,楼主是不是哪里搞错了?

operator<<的实现也要放在namespace Listsavitch里,否则要改成
ostream& operator<<(ostream& outs, const Listsavitch::List<ItemType>& the_list)
baihacker 2010-02-25
  • 打赏
  • 举报
回复
template<typename IType>
friend ostream& operator << (ostream& outs,const List<IType>& the_list);//这里出错
//如果是非绑定友元,要这样
加载更多回复(4)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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