class and operator overloading woes

I am taking programming 2 online, and I seemed to do pretty well in programming 1 but I am really struggling with classes and operator overloading. I am using Dev-C++ and it keeps giving me an error message:
34 C:\CPP\4 - 1\rectangleType.h `rectangleType rectangleType::operator++(rectangleType&, int)' must take either zero or one argument

and

35 C:\CPP\4 - 1\rectangleType.h `rectangleType rectangleType::operator--(rectangleType&, int)' must take either zero or one argument

I am also having issues with allowing the member functions to access the private variables. The assignment is to "Write the definitions of the functions to overload the increment, decrement, arithmetic, and relational operators as members of the class rectangleType, also write a program that tests operations on that class."

Here is my code:
FILE: rectangleType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//This file is originally from the book examples, but I have modified it for the assignment
#ifndef H_rectangleType
#define H_rectangleType
  
class rectangleType
{
public:
    void setDimension(double l, double w);
    double getLength() const;
    double getWidth() const;
    double area() const;
    double perimeter() const;
    void print() const;

    rectangleType operator+(const rectangleType&) const;
      //Overload the operator +
    rectangleType operator*(const rectangleType&) const;
      //Overload the operator *

    bool operator==(const rectangleType&) const;
      //Overload the operator ==
    bool operator!=(const rectangleType&) const;
      //Overload the operator !=
      
//start my code*******************************
    rectangleType operator-(const rectangleType&) const;
    rectangleType operator/(const rectangleType&) const;
    
    bool operator<(const rectangleType&) const;
    bool operator>(const rectangleType&) const;
    bool operator<=(const rectangleType&) const;
    bool operator>=(const rectangleType&) const;
    
    rectangleType operator++(rectangleType& rectangle, int z);
    rectangleType operator--(rectangleType& rectangle, int z);
    /*
    rectangleType operator++(rectangleType&, int);
    rectangleType operator--(rectangleType&, int);
    */
    

//end my code***********************************

    rectangleType();
    rectangleType(double l, double w);

private:
    double length;
    double width;
};

#endif


FILE: rectangleTypeImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//This file is originally from the book examples, but I have modified it for the assignment
#include <iostream>
#include "rectangleType.h"

using namespace std; 

void rectangleType::setDimension(double l, double w)
{
    if (l >= 0)
        length = l;
    else
        length = 0;

    if (w >= 0)
        width = w;
    else
        width = 0;
}

double rectangleType::getLength() const
{
    return length;
}

double rectangleType::getWidth()const
{
    return width;
}

double rectangleType::area() const
{
    return length * width;
}

double rectangleType::perimeter() const
{
    return 2 * (length + width);
}

void rectangleType::print() const
{
    cout << "Length = "  << length
         << "; Width = " << width;
}

rectangleType::rectangleType(double l, double w)
{
    setDimension(l, w);
}

rectangleType::rectangleType()
{
    length = 0;
    width = 0;
}

rectangleType rectangleType::operator+ 
                        (const rectangleType& rectangle) const
{
    rectangleType tempRect;

    tempRect.length = length + rectangle.length;
    tempRect.width = width + rectangle.width;

    return tempRect;
}

rectangleType rectangleType::operator* 
                        (const rectangleType& rectangle) const
{
    rectangleType tempRect;

    tempRect.length = length * rectangle.length;
    tempRect.width = width * rectangle.width;

    return tempRect;
}
//start my code*****************************

rectangleType rectangleType::operator- 
                        (const rectangleType& rectangle) const
{
    rectangleType tempRect;

    tempRect.length = length - rectangle.length;
    tempRect.width = width - rectangle.width;

    return tempRect;
}

rectangleType rectangleType::operator/ 
                        (const rectangleType& rectangle) const
{
    rectangleType tempRect;

    tempRect.length = length / rectangle.length;
    tempRect.width = width / rectangle.width;

    return tempRect;
}

//this snippet taken from the book, page 862
rectangleType operator++(rectangleType& rectangle, int z)
{
    (rectangle.length)++;
    (rectangle.width)++;
    return rectangle;
}
/*
//this snippet taken from the book, page 864
rectangleType rectangleType::operator++(rectangleType& rectangle, int)
{
    rectangleType temp = *this;
    (rectangle.length)++;
    (rectangle.width)++;
    return temp;
}
*/
//end snippets

rectangleType operator--(rectangleType rectangle, int z)
{
    (rectangle.length)--;
    (rectangle.width)--;
    return rectangle;
}
/*
rectangleType rectangleType::operator--(rectangleType& rectangle, int)
{
    rectangleType temp = *this;
    (rectangle.length)--;
    (rectangle.width)--;
    return temp;
}
*/

//end my code*********************************

bool rectangleType::operator== 
                      (const rectangleType& rectangle) const
{
    return (length == rectangle.length &&
            width == rectangle.width);
}

bool rectangleType::operator!= 
                       (const rectangleType& rectangle) const
{
    return (length != rectangle.length ||
            width != rectangle.width);
}

//start my code**********************
bool rectangleType::operator<
                       (const rectangleType& rectangle) const
{
    return (length < rectangle.length ||
            width < rectangle.width);
}

bool rectangleType::operator>
                       (const rectangleType& rectangle) const
{
    return (length > rectangle.length ||
            width > rectangle.width);
}

bool rectangleType::operator<= 
                       (const rectangleType& rectangle) const
{
    return (length <= rectangle.length ||
            width <= rectangle.width);
}

bool rectangleType::operator>= 
                       (const rectangleType& rectangle) const
{
    return (length >= rectangle.length ||
            width >= rectangle.width);
}

//end my code***************************


And finally the main program
FILE: testOpOverLoadClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//This file is originally from the book examples, but I have modified it for the assignment
//This program shows how to use the class rectangleType.

#include <iostream> 
#include "rectangleType.h"

using namespace std;

int main()
{
    rectangleType rectangle1(23, 45);               //Line 1
    rectangleType rectangle2(12, 10);               //Line 2
    rectangleType rectangle3;                       //Line 3
    rectangleType rectangle4;                       //Line 4

    cout << "Line 5: rectangle1: ";                 //Line 5
    rectangle1.print();                             //Line 6
    cout << endl;                                   //Line 7

    cout << "Line 8: rectangle2: ";                 //Line 8
    rectangle2.print();                             //Line 9
    cout << endl;                                   //Line 10

    rectangle3 = rectangle1 + rectangle2;           //Line 11 
    
    cout << "Line 12: rectangle3: ";                //Line 12
    rectangle3.print();                             //Line 13
    cout << endl;                                   //Line 14

    rectangle4 = rectangle1 * rectangle2;           //Line 15
    
    cout << "Line 16: rectangle4: ";                //Line 16
    rectangle4.print();                             //Line 17
    cout << endl;                                   //Line 18
   
    if (rectangle1 == rectangle2)                   //Line 19
        cout << "Line 20: rectangle1 and "
             << "rectangle2 are equal." << endl;    //Line 20
    else                                            //Line 21
        cout << "Line 22: rectangle1 and " 
             << "rectangle2 are not equal."
             << endl;                               //Line 22

    if (rectangle1 != rectangle3)                   //Line 23
        cout << "Line 24: rectangle1 and "
             << "rectangle3 are not equal."
             << endl;                               //Line 24
    else                                            //Line 25
        cout << "Line 25: rectangle1 and "
             << "rectangle3 are equal." << endl;    //Line 26


//start my code*****************************
    cout << endl << "--Start of my code--" << endl;
    rectangle3 = rectangle1 - rectangle2;
    cout << "Rectangle1 - Rectangle2: ";
    rectangle3.print();
    cout << endl;
    
    rectangle3 = rectangle1 / rectangle2;
    cout << "Rectangle1 / Rectangle2: ";
    rectangle3.print();
    cout << endl;
    
    
    if (rectangle1 < rectangle2)
        cout << "Line 24: rectangle1 is less than rectangle2." << endl;
    else
        cout << "Line 24: rectangle1 is NOT less than rectangle2." << endl;
        
        
    if (rectangle1 > rectangle2)
        cout << "Line 24: rectangle1 is more than rectangle2." << endl;
    else
        cout << "Line 24: rectangle1 is NOT more than rectangle2." << endl;
        
    cout << "Rectangle4 is now equal to Rectangle2" << endl;
    rectangle4 = rectangle2;
    
        if (rectangle2 <= rectangle4)
        cout << "Line 24: rectangle2 is less than or equal to rectangle4." << endl;
    else
        cout << "Line 24: rectangle2 is NOT less than or equal to rectangle4." << endl;
    
        if (rectangle2 >= rectangle4)
        cout << "Line 24: rectangle2 is greater than or equal to rectangle4." << endl;
    else
        cout << "Line 24: rectangle2 is NOT greater than or equal to rectangle4." << endl;
        
        
    cout << "Rectangle3: ";
    rectangle3.print();
    cout << endl;
    /*
    cout << "Rectangle3 after increment (++rectangle3): ";
    ++rectangle3;
    rectangle3.print();
    cout << endl;
    */
    cout << "Rectangle3 after increment (rectangle3++): ";
    rectangle3++;
    rectangle3.print();
    cout << endl;
    
    cout << "Rectangle2 set to 8,15" << endl;
    rectangle2.setdimension(8,15);
    
    cout << "Rectangle2 after decrement (--rectangle2): ";
    --rectangle2;
    rectangle2.print();
    cout << endl;
    /*
    cout << "Rectangle2 after decrement (rectangle2--); ";
    rectangle2--;
    rectangle2.print();
    cout << endl;
    */
    cout << "--End of my code--" << endl;




//end my code*************************************

    return 0;    
}


Forgive the random code omissions, I was trying to troubleshoot. I have been working at this for several hours and if I make the variables public, I only have 2 error messages (the ones listed at the beginning). Can anybody tell me what is going on here, and how to fix it? I don't understand the error message, but from the research I have done I am thinking it has something to do with my function prototype and definition. I just really need a hint on fixing this. Once I can get it to compile without those error messages, I think I can get the rest on my own. Thank you for reading!
Last edited on
Operator ++ takes zero or one arguments depending on which one you want. One of them takes a dummy int argument (I believe that is postfix but I could be wrong). Thus, your operator++ that is taking a rectangle& as well is incorrect I think.

EDIT: I just noticed that's exactly what the error message says. :P
Last edited on
THANK YOU!

It's getting past those error messages now. Hopefully I won't have issues debugging the rest of the code.

Thanks again!
Topic archived. No new replies allowed.