C++ operation overload, crash
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;
}
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).
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.
精彩评论