0

Why does this code crash the program when I run it

ostream& operator<<(ostream& cout, Array<int> a) {

    return cout;
}

and this doesn't

ostream& operator<<(ostream& cout, Array<int>& a) {

    return cout;
}
brian14708
  • 1,941
  • 2
  • 20
  • 28

2 Answers2

1

What does the copy constructor for Array<int> do ? See if reading the first answer to What is The Rule of Three? solves your problem (namely, that your class handles internally a pointer to a resource, but fails to perform a deep copy in its copy constructor, resulting in two instances deleting the same resource).

Community
  • 1
  • 1
Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
1

The overwhelming probability is that your Array<int>'s copy constructor or destructor are screwed. In addition to that, you've got some serious namespacing problems- you've used namespace std for the ostream, but then called your argument cout, which is a conflict with std::cout. I'm amazed this code compiles- you should always use std:: for Standard names, because otherwise is just ambiguous.

Puppy
  • 144,682
  • 38
  • 256
  • 465