#include<iostream>
using namespace std;
class Vector
{
float v[4];
float sum;
friend class vectorContainer;
public:
Vector(float ve[]){
sum=0;
for(int i=0;i<4;i++)
{
v[i]=ve[i];
sum=sum+v[i];
}
}
Vector(){
for(int i=0;i<4;i++)
v[i]=0;
}
~Vector(){}
void output();
Vector& operator=(Vector &a);
};
void Vector::output()
{
for(int i=0;i<4;i++)
{
cout << v[i] << " " ;
}
cout << sum << endl ;
}
Vector& Vector::operator=(Vector &a)
{
for(int i=0;i<4;i++)
{
v[i]=a.v[i];
}
sum=a.sum;
return *this;
}
class vectorContainer
{
Vector* _v;
int _size;
public:
vectorContainer(Vector *ve,int size){
_v=new Vector[size];
_size=size;
for(int i=0;i<size;i++)
_v[i]=ve[i];
}
vectorContainer(){}
~vectorContainer(){}
Vector& operator[](int idx);
Vector* operator->();
void operator()();
Vector& operator()(int i);
float& operator()(int i,int j);
};
Vector& vectorContainer::operator[](int idx)
{
_v=_v+idx;
for(int j=0;j<4;j++)
cout << _v->v[j] << " " ;
cout << endl;
_v=_v-idx;
return *_v;
}
Vector* vectorContainer::operator->()
{
Vector *big;
big=_v[0];
for(int i=1;i<_size;i++)
{
if(big.sum<_v[i].sum)
big=_v[i];
}
return big;
}
int main()
{
Vector a0[4];
float k1[]={1,2,3,4};
float k2[]={2,3,4,5};
float k3[]={3,4,5,6};
float k4[]={4,5,6,7};
Vector a1(k1);
Vector a2(k2);
Vector a3(k3);
Vector a4(k4);
a0[0]=a1;
a0[1]=a2;
a0[2]=a3;
a0[3]=a4;
vectorContainer c(a0,4);
c[0];
c[1];
c[2];
c[3];
c->output();
}
在重载—>的时候发生了不知道什么错误,麻烦帮帮我改1下,请详细解释,我已经重载了=号,并且在重载【】时验证了是正确的
相关分类