The code:
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
class Point{
double m_x,m_y;
public:
Point(double x=0,double y=0):m_x(x),m_y(y){}
Point operator+(const Point b)const{return Point(m_x+b.m_x,m_y+b.m_y);}
Point operator/(double b)const{return Point(m_x/b,m_y/b);}
const double& X() const{return m_x;}
const double& Y() const{return m_y;}
friend istream& operator>>(istream &is, Point& a);
friend ostream& operator<<(ostream &os,const Point& a);
};
istream& operator>>(istream &is, const Point&a){
return is>>a.X()>>a.Y();
}
ostream& operator<<(ostream &os,const Point&a){
return os<<"("<<a.X()<<","<<a.Y()<<")";
}
class PVec{
vector<Point> m_pts;
public:
PVec(int n):m_pts(n){}
const Point& operator[](int k) const {return m_pts[k];}
size_t size() const {return m_pts.size();}
Point sum()const{return accumulate(m_pts.begin(),m_pts.end(),Point());}
Point avg()const{return sum()/m_pts.size();}
};
istream& operator>>(istream &xin,PVec&a){
for(int k=0;k<a.size();k++)xin>>a[k];
return xin;
}
ostream& operator<<(ostream &xout,const PVec&a){
return xout<<"sum:"<<a.sum()<<endl<<"avg:"<<a.avg()<<endl;
}
int main(){
int n;cin>>n;
PVec pts(n);
cin>>pts;
cout<<pts<<endl;
return 0;
}
Input:
7 89 95 11 10 18 10 88 10 21 67 46 53 58 50
Expected output:
sum:(331,295)
avg:(47.2857,42.1429)
Actual output: none
I tried debugging and there are no bugs, but the output is just null.
The constructor
Point(double x=0,double y=0):m_x(x),m_y(y){}
Is two-in-one constructor: default and implicit converting constructor. Implicit converting constructors are almost always error prone and must be avoided if there are no specific reasons to keep it. Particularly, it causes the infinite recursion in the operator>>
istream& operator>>(istream &is, const Point&a){
return is>>a.X()>>a.Y();
}
// implements
istream& operator>>(istream &is, const Point&a){
return is>>Point(a.X())>>Point(a.Y());
}
Declare the constructor explicit
explicit Point(double x=0,double y=0):m_x(x),m_y(y){}
And you will catch the errors in the code. All issues started from that implicit two-in-one constructor.
operator>>
declaration and definition are deafferent. The latter is incorrect, since it expects to modify its argument, fixed:
istream& operator>>(istream &is, Point&a) {
a.X()
and a.Y()
are references to constant values, may not be used in the operator. The friend operator may access the class members directly:
istream& operator>>(istream& is, Point& a) {
return is >> a.m_x >> a.m_y;
}
const Point& operator[](int k) const { return m_pts[k]; }
that should be a
Point& operator[](int k) { return m_pts[k]; }
The working code as expected: https://godbolt.org/z/6MxhdGe3P