请教一个操作符重载的问题

schwarz 2006-10-18 11:31:38
下面是程序的代码:

#include <iostream.h>

template <class Type>
class datalist {
private:
Type * Element;
int ArraySize;
void swap(const int mark1,const int mark2);
int maxkey(const int low, const int high);
public:
datalist(int size=10):ArraySize(size),Element(new Type[size]) {}
~datalist() {delete [] Element;}
void sort();
friend ostream& operator<< (ostream &outstream,const datalist<Type> &outlist);
friend istream& operator>> (istream &instream, const datalist<Type> &inlist);
};

template <class Type> void datalist<Type>::swap(const int mark1,const int mark2){
Type temp = Element[mark1];
Element[mark1]=Element[mark2];
Element[mark2]=temp;
}

template <class Type> int datalist<Type>::maxkey(const int low,const int high){
int max = low;
for(int k = low + 1; k <= high; k++)
if(Element[k] > Element[max] ) max = k;
return max;
}


template <class Type> void datalist<Type>::sort(){
for(int i=ArraySize-1; i > 0; i--){
int j = maxkey(0,i);
if(j != i) swap(j,i);
}
}


template <class Type> ostream& operator<< (ostream &outstream, const datalist<Type> &outlist){
outstream << "Array contents:\n";
for(int i = 0 ; i < outlist.ArraySize; i++)
outstream << outlist.Element[i] << ' ';
outstream << endl;
outstream << "Array current size:" << outlist.ArraySize << endl;
return outstream;
}

template <class Type> istream& operator>> (istream &instream, datalist<Type> &inlist){
cout << "Enter array current size: ";
instream >> inlist.ArraySize;
cout << "Enter array elements:\n";
for(int i = 0 ; i < inlist.ArraySize; i++){
cout << "Element" << i << ":";
instream >> inlist.Element[i];
}
return instream;
}

const int SIZE = 10;

int main(){
datalist<int> testList(SIZE);
cin >> testList;
cout << "Testing selection sort:\n " << testList << endl;
testList.sort();
cout << "after sorting:\n" << testList << endl;
return 0;
}

我在vc6.0下编译时通不过,在dev-c++下也无法通过编译,
vc6.0报错如下:
test.cpp(51) : error C2248: 'ArraySize' : cannot access private member declared in class 'datalist<int>'
test.cpp(7) : see declaration of 'ArraySize'
test.cpp(65) : see reference to function template instantiation 'class istream &__cdecl operator >>(class istream &,class datalist<int> &)' being compiled
test.cpp(53) : error C2248: 'ArraySize' : cannot access private member declared in class 'datalist<int>'
test.cpp(7) : see declaration of 'ArraySize'
test.cpp(65) : see reference to function template instantiation 'class istream &__cdecl operator >>(class istream &,class datalist<int> &)' being compiled
test.cpp(55) : error C2248: 'Element' : cannot access private member declared in class 'datalist<int>'
test.cpp(6) : see declaration of 'Element'
test.cpp(65) : see reference to function template instantiation 'class istream &__cdecl operator >>(class istream &,class datalist<int> &)' being compiled
Error executing cl.exe.

请大家帮我看看,问题在哪个地方,我已经声明了<< 和 >>为datalist的友元函数,
friend ostream& operator<< (ostream &outstream,const datalist<Type> &outlist);
friend istream& operator>> (istream &instream, const datalist<Type> &inlist);

为何对datalist的private域没有访问权限呢?
...全文
146 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2006-10-18
  • 打赏
  • 举报
回复
你自己搜索herb sutter “让模板成为友元”吧。
要之,只有上述2种形式可以算正确,方式2被支持的更多些,也更符合C++标准。并且有些编译器2种形式都不支持,那么只好放弃友元用public接口。
还有一种方法就是把友元函数的实现代码直接写到你的类里面。
template <class Type>
class datalist {
。。。
friend ostream& operator<< (ostream &outstream,const datalist<Type> &outlist)
{
。。。。
}

};
schwarz 2006-10-18
  • 打赏
  • 举报
回复
friend的修饰符提供了一个非成员函数访问类中私有成员的机制

现在我不希望将类中的变量设为私有,只想某些非成员函数能够访问它, 比如像上例子中的输出输入<< >>操作符,通过friend操作符是应该可以实现的阿

为何通不过呢?

taodm说的,不是特别明白,可以这样理解你的意思吗?
为了在一个类中声明一个非成员函数为友元,是不是一定要将这个函数在namespace中声明呢?
好像不是这样的吧
飞哥 2006-10-18
  • 打赏
  • 举报
回复
模板可以是友元
只要提前声明就可以好像
----------
不信你就baidu一下
飞哥 2006-10-18
  • 打赏
  • 举报
回复
这时你的getArraySize()
就相当于你的ArraySize了
taodm 2006-10-18
  • 打赏
  • 举报
回复
这个可看herb sutter的“让模板成为友元”
namespace boost {
template<typename T> void checked_delete( T* x );
}
class Test {
friend void boost::checked_delete ( Test* x ); // BAD
friend void boost::checked_delete<>( Test* x ); // GOOD
};

如果你的编译器不支持这两个友元申明中的任何一个,那么,你不得不将必须的函数设为公有-但加一个注释解释为什么这么做,并提醒只要一升级你的编译器就将它改回私有。[注1]
飞哥 2006-10-18
  • 打赏
  • 举报
回复
私有成员只有在类内使用
就是说必须在成员函数内使用
----------------
你在类外不能操作私有数据
不然这个私有数据就没有意义了
要不就设置为共有的public
要不提供个接口来取这个数据
比如
int getArraySize()
{
return ArraySize;
}
OOPhaisky 2006-10-18
  • 打赏
  • 举报
回复
楼主如果想了解模板友元的相关写法,可以参考c++ primer 16.4节。

64,654

社区成员

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

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