Overloading subtraction operator cpp
I'm a bit new to object oriented programming in c++ and I've been trying to overload subtraction(-) operator in c++ for a Complex class I created. It is working fine except my program is terminating abnormally.
Below is what I've been trying to do:
#include<iostream>
#include<cstdlib>
class Complex{
//Data-members
private:
int re, im;
//methods
public:
//Constructor
Complex(){ /*default Constructor*/ }
Complex(const int& re_, const int& im_):re(re_), im(im_){}
//Subtraction(-) operator overloading
Complex operator-(const Complex& op)
{
Complex res(this->re - op.re, this->im - op.im);
return res;
}
//get-set methods for re
int getReal(){ return re; }
void setReal(const int& re){ this->re = re; }
//get-set methods for im
int getImaginary(){ return im; }
void setImaginary(const int& im){ this->im = im; }
//Destructor
~Complex(){ free(this); }
};
int main()
{
Complex a(2, 3), b(3, 5);
Complex d = a - b;
std::cout<<"d.re = "<<d.getReal()<<" d.im = "<<d.getImaginary()<<"n";
return 0;
}
Can anyone please explain the cause of error.
c++
|
show 2 more comments
I'm a bit new to object oriented programming in c++ and I've been trying to overload subtraction(-) operator in c++ for a Complex class I created. It is working fine except my program is terminating abnormally.
Below is what I've been trying to do:
#include<iostream>
#include<cstdlib>
class Complex{
//Data-members
private:
int re, im;
//methods
public:
//Constructor
Complex(){ /*default Constructor*/ }
Complex(const int& re_, const int& im_):re(re_), im(im_){}
//Subtraction(-) operator overloading
Complex operator-(const Complex& op)
{
Complex res(this->re - op.re, this->im - op.im);
return res;
}
//get-set methods for re
int getReal(){ return re; }
void setReal(const int& re){ this->re = re; }
//get-set methods for im
int getImaginary(){ return im; }
void setImaginary(const int& im){ this->im = im; }
//Destructor
~Complex(){ free(this); }
};
int main()
{
Complex a(2, 3), b(3, 5);
Complex d = a - b;
std::cout<<"d.re = "<<d.getReal()<<" d.im = "<<d.getImaginary()<<"n";
return 0;
}
Can anyone please explain the cause of error.
c++
Whatfree(this);
is supposed to do? It's unnecessary and causes the error.
– HolyBlackCat
Nov 14 '18 at 11:55
Also, why passconst int&
instead of a plainint
?
– HolyBlackCat
Nov 14 '18 at 11:56
1
Remove thefree(this)
from the destructor. Callingfree()
with an argument that was not returned bymalloc()
(or related functions, likecalloc()
orrealloc()
) gives undefined behaviour. There is nothing in your code returned bymalloc()
, and doesn't need to be.
– Peter
Nov 14 '18 at 11:57
See also rule of three here
– anatolyg
Nov 14 '18 at 11:58
1
@anatolyg - rule of zero is more applicable here. The class doesn't explicitly manage any resource, compiler-supplied default implementations of copy constructor, assignment operator, and destructor are fine.
– Peter
Nov 14 '18 at 12:00
|
show 2 more comments
I'm a bit new to object oriented programming in c++ and I've been trying to overload subtraction(-) operator in c++ for a Complex class I created. It is working fine except my program is terminating abnormally.
Below is what I've been trying to do:
#include<iostream>
#include<cstdlib>
class Complex{
//Data-members
private:
int re, im;
//methods
public:
//Constructor
Complex(){ /*default Constructor*/ }
Complex(const int& re_, const int& im_):re(re_), im(im_){}
//Subtraction(-) operator overloading
Complex operator-(const Complex& op)
{
Complex res(this->re - op.re, this->im - op.im);
return res;
}
//get-set methods for re
int getReal(){ return re; }
void setReal(const int& re){ this->re = re; }
//get-set methods for im
int getImaginary(){ return im; }
void setImaginary(const int& im){ this->im = im; }
//Destructor
~Complex(){ free(this); }
};
int main()
{
Complex a(2, 3), b(3, 5);
Complex d = a - b;
std::cout<<"d.re = "<<d.getReal()<<" d.im = "<<d.getImaginary()<<"n";
return 0;
}
Can anyone please explain the cause of error.
c++
I'm a bit new to object oriented programming in c++ and I've been trying to overload subtraction(-) operator in c++ for a Complex class I created. It is working fine except my program is terminating abnormally.
Below is what I've been trying to do:
#include<iostream>
#include<cstdlib>
class Complex{
//Data-members
private:
int re, im;
//methods
public:
//Constructor
Complex(){ /*default Constructor*/ }
Complex(const int& re_, const int& im_):re(re_), im(im_){}
//Subtraction(-) operator overloading
Complex operator-(const Complex& op)
{
Complex res(this->re - op.re, this->im - op.im);
return res;
}
//get-set methods for re
int getReal(){ return re; }
void setReal(const int& re){ this->re = re; }
//get-set methods for im
int getImaginary(){ return im; }
void setImaginary(const int& im){ this->im = im; }
//Destructor
~Complex(){ free(this); }
};
int main()
{
Complex a(2, 3), b(3, 5);
Complex d = a - b;
std::cout<<"d.re = "<<d.getReal()<<" d.im = "<<d.getImaginary()<<"n";
return 0;
}
Can anyone please explain the cause of error.
c++
c++
edited Nov 14 '18 at 13:09
Ted Lyngmo
2,8112317
2,8112317
asked Nov 14 '18 at 11:52
user9999486user9999486
15
15
Whatfree(this);
is supposed to do? It's unnecessary and causes the error.
– HolyBlackCat
Nov 14 '18 at 11:55
Also, why passconst int&
instead of a plainint
?
– HolyBlackCat
Nov 14 '18 at 11:56
1
Remove thefree(this)
from the destructor. Callingfree()
with an argument that was not returned bymalloc()
(or related functions, likecalloc()
orrealloc()
) gives undefined behaviour. There is nothing in your code returned bymalloc()
, and doesn't need to be.
– Peter
Nov 14 '18 at 11:57
See also rule of three here
– anatolyg
Nov 14 '18 at 11:58
1
@anatolyg - rule of zero is more applicable here. The class doesn't explicitly manage any resource, compiler-supplied default implementations of copy constructor, assignment operator, and destructor are fine.
– Peter
Nov 14 '18 at 12:00
|
show 2 more comments
Whatfree(this);
is supposed to do? It's unnecessary and causes the error.
– HolyBlackCat
Nov 14 '18 at 11:55
Also, why passconst int&
instead of a plainint
?
– HolyBlackCat
Nov 14 '18 at 11:56
1
Remove thefree(this)
from the destructor. Callingfree()
with an argument that was not returned bymalloc()
(or related functions, likecalloc()
orrealloc()
) gives undefined behaviour. There is nothing in your code returned bymalloc()
, and doesn't need to be.
– Peter
Nov 14 '18 at 11:57
See also rule of three here
– anatolyg
Nov 14 '18 at 11:58
1
@anatolyg - rule of zero is more applicable here. The class doesn't explicitly manage any resource, compiler-supplied default implementations of copy constructor, assignment operator, and destructor are fine.
– Peter
Nov 14 '18 at 12:00
What
free(this);
is supposed to do? It's unnecessary and causes the error.– HolyBlackCat
Nov 14 '18 at 11:55
What
free(this);
is supposed to do? It's unnecessary and causes the error.– HolyBlackCat
Nov 14 '18 at 11:55
Also, why pass
const int&
instead of a plain int
?– HolyBlackCat
Nov 14 '18 at 11:56
Also, why pass
const int&
instead of a plain int
?– HolyBlackCat
Nov 14 '18 at 11:56
1
1
Remove the
free(this)
from the destructor. Calling free()
with an argument that was not returned by malloc()
(or related functions, like calloc()
or realloc()
) gives undefined behaviour. There is nothing in your code returned by malloc()
, and doesn't need to be.– Peter
Nov 14 '18 at 11:57
Remove the
free(this)
from the destructor. Calling free()
with an argument that was not returned by malloc()
(or related functions, like calloc()
or realloc()
) gives undefined behaviour. There is nothing in your code returned by malloc()
, and doesn't need to be.– Peter
Nov 14 '18 at 11:57
See also rule of three here
– anatolyg
Nov 14 '18 at 11:58
See also rule of three here
– anatolyg
Nov 14 '18 at 11:58
1
1
@anatolyg - rule of zero is more applicable here. The class doesn't explicitly manage any resource, compiler-supplied default implementations of copy constructor, assignment operator, and destructor are fine.
– Peter
Nov 14 '18 at 12:00
@anatolyg - rule of zero is more applicable here. The class doesn't explicitly manage any resource, compiler-supplied default implementations of copy constructor, assignment operator, and destructor are fine.
– Peter
Nov 14 '18 at 12:00
|
show 2 more comments
1 Answer
1
active
oldest
votes
Never ever do free(this)
, least of all in the destructor. The memory for the objects will be free'd outside of the destructor, either by the compiler generated code or by the user doing delete
or delete
.
In fact this is the cause of your problem, as the objects created never were allocated with malloc
.
The proper solution in this case is to not only remove the free
call, but the whole destructor, since it's not needed for this class.
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
@user9999486delete
must match anew
, adelete
must match anew
. Nowhere in your code do you usenew
, so there shouldn't be anydelete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.
– Some programmer dude
Nov 14 '18 at 12:05
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299631%2foverloading-subtraction-operator-cpp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Never ever do free(this)
, least of all in the destructor. The memory for the objects will be free'd outside of the destructor, either by the compiler generated code or by the user doing delete
or delete
.
In fact this is the cause of your problem, as the objects created never were allocated with malloc
.
The proper solution in this case is to not only remove the free
call, but the whole destructor, since it's not needed for this class.
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
@user9999486delete
must match anew
, adelete
must match anew
. Nowhere in your code do you usenew
, so there shouldn't be anydelete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.
– Some programmer dude
Nov 14 '18 at 12:05
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
|
show 2 more comments
Never ever do free(this)
, least of all in the destructor. The memory for the objects will be free'd outside of the destructor, either by the compiler generated code or by the user doing delete
or delete
.
In fact this is the cause of your problem, as the objects created never were allocated with malloc
.
The proper solution in this case is to not only remove the free
call, but the whole destructor, since it's not needed for this class.
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
@user9999486delete
must match anew
, adelete
must match anew
. Nowhere in your code do you usenew
, so there shouldn't be anydelete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.
– Some programmer dude
Nov 14 '18 at 12:05
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
|
show 2 more comments
Never ever do free(this)
, least of all in the destructor. The memory for the objects will be free'd outside of the destructor, either by the compiler generated code or by the user doing delete
or delete
.
In fact this is the cause of your problem, as the objects created never were allocated with malloc
.
The proper solution in this case is to not only remove the free
call, but the whole destructor, since it's not needed for this class.
Never ever do free(this)
, least of all in the destructor. The memory for the objects will be free'd outside of the destructor, either by the compiler generated code or by the user doing delete
or delete
.
In fact this is the cause of your problem, as the objects created never were allocated with malloc
.
The proper solution in this case is to not only remove the free
call, but the whole destructor, since it's not needed for this class.
edited Nov 14 '18 at 12:18
answered Nov 14 '18 at 11:55
Some programmer dudeSome programmer dude
300k25258422
300k25258422
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
@user9999486delete
must match anew
, adelete
must match anew
. Nowhere in your code do you usenew
, so there shouldn't be anydelete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.
– Some programmer dude
Nov 14 '18 at 12:05
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
|
show 2 more comments
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
@user9999486delete
must match anew
, adelete
must match anew
. Nowhere in your code do you usenew
, so there shouldn't be anydelete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.
– Some programmer dude
Nov 14 '18 at 12:05
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
using delete is also terminating the program abnormally.
– user9999486
Nov 14 '18 at 11:59
@user9999486
delete
must match a new
, a delete
must match a new
. Nowhere in your code do you use new
, so there shouldn't be any delete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.– Some programmer dude
Nov 14 '18 at 12:05
@user9999486
delete
must match a new
, a delete
must match a new
. Nowhere in your code do you use new
, so there shouldn't be any delete
. As I said, it will be done automatically by the compiler since it is the compiler that allocates the objects.– Some programmer dude
Nov 14 '18 at 12:05
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
so there is no need for the destructor. Right ???
– user9999486
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
@user9999486 No, not in this example anyway.
– Some programmer dude
Nov 14 '18 at 12:08
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
how do we know that we need to add an explicit destructor in a class
– user9999486
Nov 14 '18 at 12:09
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299631%2foverloading-subtraction-operator-cpp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What
free(this);
is supposed to do? It's unnecessary and causes the error.– HolyBlackCat
Nov 14 '18 at 11:55
Also, why pass
const int&
instead of a plainint
?– HolyBlackCat
Nov 14 '18 at 11:56
1
Remove the
free(this)
from the destructor. Callingfree()
with an argument that was not returned bymalloc()
(or related functions, likecalloc()
orrealloc()
) gives undefined behaviour. There is nothing in your code returned bymalloc()
, and doesn't need to be.– Peter
Nov 14 '18 at 11:57
See also rule of three here
– anatolyg
Nov 14 '18 at 11:58
1
@anatolyg - rule of zero is more applicable here. The class doesn't explicitly manage any resource, compiler-supplied default implementations of copy constructor, assignment operator, and destructor are fine.
– Peter
Nov 14 '18 at 12:00