开发者

C++ operation overload, crash

开发者 https://www.devze.com 2023-01-30 08:29 出处:网络
Why does this开发者_开发知识库 code crash the program when I run it ostream& operator<<(ostream& cout, Array<int> a) {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消