关于类模板重载<<

谁学逆向工程 2010-05-18 04:48:35
template<typename T>
class list
{
public:
list(int size,int element);
~list(){delete [] p;cout<<"destructor list"<<endl;}
T operator[](int a);
T * p;
};
template<typename T>
list<T>::list(int size, int element)
{
p = new T[size];
for(int i = 0; i < size; i++)
(p[i].f) = element;
}
template<typename T>
T list<T>::operator[](int a)
{
return p[a];
}




class foo
{
public:
foo(){}
foo(int X){f=X;}
// friend operator<<(ostream&,foo&); int f;
};
ostream & operator << (ostream & o, foo & F)
{
o<<F.f;
return o;
}




void main()
{
list<foo> b(10,123);
cout<<b[5];
int PPP;cin>>PPP;
}
这里关于重载<<有些问题。第一在foo类定义里,为什么不能弄成友元的。第二,目前这种写法是全局的,第二个参数接受一个foo类型,这不灵活,能否把他加到类模板里,使他接受一个T类型
...全文
190 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
谁学逆向工程 2010-05-18
  • 打赏
  • 举报
回复
template<typename T>
class list
{
public:
list(int size,int element);
~list(){delete [] p;cout<<"destructor list"<<endl;}
T operator[](int a);
friend ostream & operator<<(ostream&, T&);
T * p;
};
template<typename T>
list<T>::list(int size, int element)
{
p = new T[size];
for(int i = 0; i < size; i++)
(p[i].f) = element;
}
template<typename T>
T list<T>::operator[](int a)
{
return p[a];
}
template<typename T>
ostream & operator<<(ostream & o , T & t)
{
o<<t;
return o;
}

1>------ 已启动生成: 项目: 3, 配置: Debug Win32 ------
1>正在编译...
1>3.cpp
1>c:\users\administrator\desktop\3\3\3\tem.h(6) : error C2593: “operator <<”不明确
1> c:\users\administrator\desktop\3\3\3\tem.h(24): 可能是“std::ostream &operator <<<const char[16]>(std::ostream &,T (&))”
1> with
1> [
1> T=const char [16]
1> ]
1> d:\vc++ 2008\vc\include\ostream(825): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const _Elem *)”
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\vc++ 2008\vc\include\ostream(738): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)”
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\vc++ 2008\vc\include\ostream(653): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)”
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> 试图匹配参数列表“(std::ostream, const char [16])”时
1> c:\users\administrator\desktop\3\3\3\tem.h(6): 编译类 模板 成员函数“list<T>::~list(void)”时
1> with
1> [
1> T=foo
1> ]
1> c:\users\administrator\desktop\3\3\3\3.cpp(15): 参见对正在编译的类 模板 实例化“list<T>”的引用
1> with
1> [
1> T=foo
1> ]
1>生成日志保存在“file://c:\Users\Administrator\Desktop\3\3\3\Debug\BuildLog.htm”
1>3 - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
pengzhixi 2010-05-18
  • 打赏
  • 举报
回复
如果你想,那么可以将这个类内嵌到list里面去。
linsen_519 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaoyuanyuan2009 的回复:]

引用 2 楼 pengzhixi 的回复:
自己对比吧。

老大,你到底改了哪?难道只是个const的区别吗
[/Quote]
哈哈
谁学逆向工程 2010-05-18
  • 打赏
  • 举报
回复
找到了。那里落了一个返回类型。
但是能不能用T来写啊
谁学逆向工程 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pengzhixi 的回复:]
自己对比吧。
[/Quote]
老大,你到底改了哪?难道只是个const的区别吗
linsen_519 2010-05-18
  • 打赏
  • 举报
回复
friend operator<<(ostream&,foo&);没有这么写的~
可以写成friend啊
friend ostream & operator<<(ostream&,const foo&);
pengzhixi 2010-05-18
  • 打赏
  • 举报
回复
自己对比吧。
pengzhixi 2010-05-18
  • 打赏
  • 举报
回复

template<typename T>
class list
{
public:
list(int size,int element);
~list(){delete [] p;cout<<"destructor list"<<endl;}
T operator[](int a);
T * p;
};
template<typename T>
list<T>::list(int size, int element)
{
p = new T[size];
for(int i = 0; i < size; i++)
(p[i].f) = element;
}
template<typename T>
T list<T>::operator[](int a)
{
return p[a];
}




class foo
{
public:
foo(){}
foo(int X){f=X;}
friend ostream & operator<<(ostream&,const foo&); int f;
};
ostream & operator << (ostream & o, const foo & F)
{
o<<F.f;
return o;
}




int main()
{
list<foo> b(10,123);
cout<<b[5];
int PPP;cin>>PPP;
system("pause");
return 0;
}

64,646

社区成员

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

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