overloading operator- Still need help

I have to make a overloading operator- in my program, I know how to make a overloading operator++ and -- but I don't know how to make + and -. Can someone help me?
I stored 3 members in 2 classes and I want to subtract them in the operator-.
I found that I cannot subtract the members by using box1.length - box2.length.
What can I do to subtract the members?
For example:
box1
length = 10
wide = 20
height = 30

box2
length = 30
wide = 20
height = 10

Then I write this
1
2
3
4
5
6
7
8
9
10
class Date
{
private:
int length, height, wide;
  Box& Box::operator-()
{
  length = box1.length - box2.length; //Compiler error there
  wide = box1.wide - box2.wide;       //Same error
  height = box1.height - box2.height; //Same error
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
class Date
{
private:
int length, height, wide;
  Box& Box::operator-()
{
  length = box1.length - box2.length; //Compiler error there
  wide = box1.wide - box2.wide;       //Same error
  height = box1.height - box2.height; //Same error
}
Sorry, did you change anything? Can you tell me what you change?
Hi,

Why is the operator defined in the Date class?

Why is it private?

You promise to return a reference to Box, but don't.

You haven't any parameters in the function definition.

http://en.cppreference.com/w/cpp/language/operators
Sorry, did you change anything? Can you tell me what you change?
What integralfx tried to point out is: Why is the name of your class Date?

Box& Box::operator-()
Why this scope operator?
Topic archived. No new replies allowed.