I overloaded operator << for my dynamic array Tvector container and i want to cout areas of squares.
std::ostream & operator<<(std::ostream &os, TVector &s) {
for(int i = 0; i < s.length(); ++i){
os << "[" << s[i].Area() << " ";
}
os << "]";
return os;
}
Function area
double Square::Area() {
double len_a = a.dist(b);
double len_b = b.dist(c);
return len_a * len_b;
}
If i just
cout << a.Area();
everything works fine. But if i want to cout dynamic array, compilator throws segmentation fault error. main function fragment
Square a;
std::cin >> a;
Square a1;
std::cin >> a1;
TVector v;
v.push_back(a);
v.push_back(a1);
std::cout << v;
P.S Code for TVector
TVector::TVector()
: _array(nullptr)
, _size(0)
{
int TVector::length() {
return _size;
}
Square &TVector::operator[](int idx) {
return _array[idx];
}
std::ostream & operator<<(std::ostream &os, TVector &s) {
for(int i = 0; i < s.length(); ++i){
os << "[" << s[i].Area() << " ";
}
os << "]";
return os;
}
std::istream& operator>>(std::istream& is, TVector& s) {
Square a;
is >> a;
s.push_back(a);
return is;
}
TVector::~TVector()
{
clear();
}