私有成员不能用类成员函数修改?# Programming - 葵花宝典
m*o
1 楼
如果注释private,编译就能通过
#include
using namespace std;
template
class c_array {
//friend int sum(c_array &a);
public:
c_array(T s): size(s) { a = new T[size]; }
T& operator[](int i);
int get_size() { return size; }
bool operator < (c_array &x);
private:
int size;
int *a;
};
template
T sum(c_array& a){
T result = 0;
int size = a.get_size();
for ( int i = 0; i < size; i++ )
result += a[i];
return result;
}
#include
using namespace std;
template
class c_array {
//friend int sum(c_array &a);
public:
c_array(T s): size(s) { a = new T[size]; }
T& operator[](int i);
int get_size() { return size; }
bool operator < (c_array
private:
int size;
int *a;
};
template
T sum(c_array
T result = 0;
int size = a.get_size();
for ( int i = 0; i < size; i++ )
result += a[i];
return result;
}