Operator Overloading issue

Hey everyone,

My professor insists that for every operator I overload I should have:
1.) A member function
2.) A corresponding overloaded equivalent

The actual operator overloading function is just a call to the member function.

I have operator+ and operator* down, but now I can't get operator== to compile.

Once I see what I'm doing wrong with the operator==, I'm sure the rest will fall in line.

You can look at my operator+ function to see how it should look.

Here's my .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
#ifndef HugeInteger_h
#define HugeInteger_h

#include <string>

//Course class definition
class HugeInteger
{
        public:
                HugeInteger();
                HugeInteger(int[]);

                HugeInteger operator + (const HugeInteger&);
                HugeInteger add(HugeInteger);

                HugeInteger operator * (const HugeInteger&);
                HugeInteger mult(HugeInteger);

                bool operator == (const HugeInteger&);
                bool isEqualTo(const HugeInteger&);

                bool operator != (const HugeInteger&);
                bool isNotEqualTo(const HugeInteger);

                bool operator >= (const HugeInteger&);
                bool isGreaterOrEqual(const HugeInteger);

                bool operator <= (const HugeInteger&);
                bool isLessorEqual(const HugeInteger);

                bool isZero(const HugeInteger);

                friend ostream & operator << (ostream&, const HugeInteger&);
                friend istream & operator >> (istream&, HugeInteger&);//Since cin alters data, it can't be constant!

                int atoi(char);

         private:
         //       void add(const int[]);

                static const int MAX = 40;
                static const int MAX2 = 39; // Typing (MAX - 1) is tiresome

                int arr[MAX];
};
#endif 


And here's my .cpp (I trimmed it down a bit)
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
#include <iostream>
using namespace std;

#include "HugeInteger.h"

HugeInteger::HugeInteger()
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = 0;
}

HugeInteger::HugeInteger(int op1[])
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = op1[i];
}

HugeInteger HugeInteger::operator + (const HugeInteger& right)
{
        return (add(right));
}

HugeInteger HugeInteger::add(HugeInteger op2)
{
        HugeInteger res;
        int carry = 0;

        for (int i = MAX2; i >= 0; i--)
        {
                res.arr[i] = arr[i] + op2.arr[i] + carry;
//              arr[i] = arr[i] + op2.arr[i] + carry;

                carry = 0;

                while (res.arr[i] >= 10)
                {
                        res.arr[i] -= 10;
                        carry++;
                }
        }
        return (res);
}


bool operator == (const HugeInteger& exactEqual);
{
        return(isEqualTo(exactEqual));
}

bool HugeInteger::isEqualTo(const HugeInteger& op2);
{
        bool arrayEqual = false;

        for (int i = 0; i < op2.MAX2; i++)
        {
                if(arr[i] == op.arr[i])
        }               arrayEqual = true;

        return (arrayEqual);
}


Here's the error output:

HugeInteger.cpp:161:48: error: âbool operator==(const HugeInteger&)â must take exactly two arguments
HugeInteger.cpp:162:1: error: expected unqualified-id before â{â token
HugeInteger.cpp:166:51: error: declaration of âbool HugeInteger::isEqualTo(const HugeInteger&)â outside of class is not definition [-fpermissive]
HugeInteger.cpp:167:1: error: expected unqualified-id before â{â token

Thanks in advance.
The error output's line numbers no longer match your code, but I suspect it's because you have semi-colons on lines 45 and 50 ;-)

Jim
Line 45, you forgot to qualify the operator with the class name.
1
2
3
bool HugeInteger::operator == (const HugeInteger& exactEqual);
{   return(isEqualTo(exactEqual));
}


Suggestion on your comparison routine:
1
2
3
4
5
6
7
bool HugeInteger::isEqualTo(const HugeInteger& op2);
{   for (int i = 0; i < op2.MAX2; i++)
     {  if(arr[i] != op2.arr[i])
             return false;   // return immediately on miscompare.  No point to continue
      }
    return true;  // Can only be true if both arrays matched in the entirity.
}

p.s. Line 56 should refer to op2.arr, not op.arr


Haha, good catch.

I fixed that, along with the misplaced parenthesis in line 57 and the "op.arr" in line 56 should be "op2.arr"

After that, I only got one compiler error, but it's one I don't how to fix:

HugeInteger.cpp:161:48: error: âbool operator==(const HugeInteger&)â must take exactly two arguments
The correct signature of operator== is

 
bool MyClass::operator==(const MyClass& other) const;


Jim
A good source for operator overloading is http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html

Jim
Topic archived. No new replies allowed.