请教一个操作符重载的问题
下面是程序的代码:
#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域没有访问权限呢?