模板类的友元问题,有个问题条是不通过,请教

shdiao 2003-09-28 10:03:29
原码如下:
#include <iostream>
#include<string>
using namespace std;
template <class T>
class Array
{
private:
T* pArray;
int size;

public:
Array(int nSize=30);
Array(const Array<T> &x);
~Array(){delete []pArray;}
Array<T> &operator=(const Array<T> &x);
T &operator[](int nCount);
int getSize(){return size;}
friend ostream& operator<<(ostream&, const Array<T>&);

};
template <class T>
Array<T>::Array(int nSize)
{
size=nSize;
pArray=new T[size];
if(pArray==0)
{
cout<<"MEMORY OUT!!"<<endl;
}
}
template <class T>
Array<T>::Array(const Array<T>&x)
{
size=x.size;
delete []pArray;
pArray=new T[size];
T *srcptr=x.pArray;
T *destptr=pArray;
int temp=size;
while(temp)
{
temp--;
destptr[temp]=srcptr[temp];
}
}
template <class T>
Array<T>& Array<T>::operator=(const Array<T> &x)
{
delete []pArray;
pArray=new T<x.getSize()>
T *srcptr=x.pArray;
T *destptr=pArray;
int temp=size;
while(temp)
{
temp--;
destptr[temp]=srcptr[temp];
}
return *this;
}
template <class T>
T & Array<T>::operator[](int nCount)
{
return pArray[nCount];
}
template <class T>
ostream& operator<<(ostream& os, const Array<T>& a)
{
for(int i=0;i<a.size; i++)
{
os<<a.pArray[i];
}
os<<endl;
return os;
}
///////////////////////////////////////
以下为测试程序:
void main()
{
Array<int> a(20);
for(int i=0;i<20;i++)
{
a[i]=i+1;
}
Array<int> b(a),c=a;
cout<<a;
}
//////////////////////////////////////
以下为错误信息:错误出在重载输出操作符上。
Compiling...
a.cpp
g:\download1\专用上传文件夹\daodao\a.cpp(69) : error C2248: 'size' : cannot access private member declared in class 'Array<int>'
g:\download1\专用上传文件夹\daodao\a.cpp(9) : see declaration of 'size'
g:\download1\专用上传文件夹\daodao\a.cpp(84) : see reference to function template instantiation 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl operator <<(class std::basic_ostream<char,struct std::char_traits<char> >
&,const class Array<int> &)' being compiled
g:\download1\专用上传文件夹\daodao\a.cpp(71) : error C2248: 'pArray' : cannot access private member declared in class 'Array<int>'
g:\download1\专用上传文件夹\daodao\a.cpp(8) : see declaration of 'pArray'
g:\download1\专用上传文件夹\daodao\a.cpp(84) : see reference to function template instantiation 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl operator <<(class std::basic_ostream<char,struct std::char_traits<char> >
&,const class Array<int> &)' being compiled
Error executing cl.exe.

a.obj - 2 error(s), 0 warning(s)
...全文
38 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
panzhaoping 2003-10-25
  • 打赏
  • 举报
回复
vc7.0行的
6.0要装补丁的
helloair 2003-09-29
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2304/2304340.xml?temp=.3737299
yizhili 2003-09-29
  • 打赏
  • 举报
回复
还有个方法嘛,利用getsize和operator[]就可以不用友函数了。
yizhili 2003-09-29
  • 打赏
  • 举报
回复
好像不对。难道是因为VC6对模板的支持不好?
yizhili 2003-09-29
  • 打赏
  • 举报
回复
对哟,好像不用Array<T>,Array就可以了吧?
Array(const Array<T> &x);
和 Array<T> &operator=(const Array<T> &x);
也是,申明中不用-个个写<T>的。
ljianq 2003-09-29
  • 打赏
  • 举报
回复
楼主的代码在C++Builder6下通过,没有问题啊。

上面两位朋友:
在类中template<class T>
friend ostream& operator<<(ostream&, const Array<T>&);
这样声明有问题吧。
短歌如风 2003-09-29
  • 打赏
  • 举报
回复
VC6对模板类模板函数友元处理有问题。
我一般不用友元,这样:
template<typename T>
class A
{
...
public:
ostream& Output(ostream& strm)const
{
//这里实现输出
}
};

template <typename T>
ostream& operator<<(ostream& strm, const A<T>& a)
{
return a.Output(strm);
}
aflyinghorse 2003-09-28
  • 打赏
  • 举报
回复
同意楼上

operator=内应改为
template <class T>
Array<T>& Array<T>::operator=(const Array<T> &x)
{
if (this != &x)
{
delete []pArray;
pArray=new T<x.getSize()>
T *srcptr=x.pArray;
T *destptr=pArray;
int temp=size;
while(temp)
{
temp--;
destptr[temp]=srcptr[temp];
}
}
return *this;
}

还有构造函数内应改为捕捉异常
new分配失败后不返回0
sevecol 2003-09-28
  • 打赏
  • 举报
回复
改成这样:(在VC7.1下通过)

1 类中的友元函数声明改成
template<class T> //你的函数是摸班函数不是普通函数,需要template<class T>
friend ostream& operator<<(ostream&, const Array<T>&);

2 拷贝构造函数内
去掉下面的这一句
delete []pArray;

3 在operator=内
加上下面的判断
if (this==&x)
return;

24,855

社区成员

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

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