有关友元模板函数的问题

lnklo 2009-12-12 01:04:57
因为这个函数vc++一直说 'void' function returning a value
template<class t>
ostream &operator<<(ostream &thestream,array<t> ta)
{
for(int i=0;i<ta.gets();++i)
thestream<<ta.p[i]<<endl;
return thestream;
}

以下完整代码
#include<iostream>
using namespace std;
const int defaultsize=10;
class animal
{
public:
animal(int weight):i(weight){}
animal():i(0){}
~animal(){}
int geti(){return i;}
void display(){cout<<i<<endl;}
private:
int i;
};
template<class t>
class array
{
public:
int gets()const{return s;}
const t &operator[](int offset)const
{return p[offset];}
t &operator[](int offset){return p[offset];}
array(int size=defaultsize):
s(size){
p=new t(s);
for(int i=0;i<s;++i)
p[i]=0;
}
array(const array &rhs)
{
s=rhs.gets();
p=new t[s];
for(int i=0;i<s;++i)
p[i]=rhs[i];
}
array &operator=(const array &rhs)
{
if(this=&rhs)
return *this;
delete [] p;
s=rhs.gets();
p=new t[s];
for(int i=0;i<s;++i)
p[i]=rhs[i];
}
template<class t>
friend ostream &operator<<(ostream &thestream,array<t> ta);
private:
t *p;
int s;
};
template<class t>
ostream &operator<<(ostream &thestream,array<t> ta)
{
for(int i=0;i<ta.gets();++i)
thestream<<ta.p[i]<<endl;
return thestream;
}

int main()
{
array<int> ta;
int offset,value;
while(true)
{
cout<<"erter two number:";
cin>>offset>>value;
if(offset<0)
break;
ta[offset]=value;
}
cout<<ta;
return 0;
}
...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2009-12-12
  • 打赏
  • 举报
回复
vc6的著名bug……
请更换编译器……
lnklo 2009-12-12
  • 打赏
  • 举报
回复
谢LS的各位
但还是不行

ostream &operator < <(ostream &thestream,array <t> ta)
{
for(int i=0;i <ta.gets();++i)
thestream < <ta.p[i] < <endl;//这里无法访问私有成员
return thestream;
}
benbshmily 2009-12-12
  • 打赏
  • 举报
回复

array(int size=defaultsize):
s(size){
p=new t(s);//p=new t[s];应该像这样分配,你那样是分配一个t类型空间。
for(int i=0;i <s;++i)
p[i]=0;
}
飞天御剑流 2009-12-12
  • 打赏
  • 举报
回复
模板<<重载函数跟类模板array使用了相同名字的参数化类型,把<<的改一改。
mstlq 2009-12-12
  • 打赏
  • 举报
回复
修改一处……
gcc编译通过

#include <iostream>
using namespace std;
const int defaultsize=10;
class animal
{
public:
animal(int weight):i(weight){}
animal():i(0){}
~animal(){}
int geti()
{
return i;
}
void display()
{
cout <<i <<endl;
}
private:
int i;
};
template <class t>
class array
{
public:
int gets()const
{
return s;
}
const t &operator[](int offset)const
{
return p[offset];
}
t &operator[](int offset)
{
return p[offset];
}
array(int size=defaultsize):
s(size)
{
p=new t(s);
for (int i=0;i <s;++i)
p[i]=0;
}
array(const array &rhs)
{
s=rhs.gets();
p=new t[s];
for (int i=0;i <s;++i)
p[i]=rhs[i];
}
array &operator=(const array &rhs)
{
if (this=&rhs)
return *this;
delete [] p;
s=rhs.gets();
p=new t[s];
for (int i=0;i <s;++i)
p[i]=rhs[i];
}
template <class U>//mark
friend ostream &operator <<(ostream &thestream,array <U> ta);
private:
t *p;
int s;
};
template <class t>
ostream &operator <<(ostream &thestream,array <t> ta)
{
for (int i=0;i <ta.gets();++i)
thestream <<ta.p[i] <<endl;
return thestream;
}

int main()
{
array <int> ta;
int offset,value;
while (true)
{
cout <<"erter two number:";
cin>>offset>>value;
if (offset <0)
break;
ta[offset]=value;
}
cout <<ta;
return 0;
}

64,282

社区成员

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

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