Problems calling overloaded functions

I believe I have the syntax correct but I'm having difficulty calling my overloaded == operator in main (last snip-it of code). Below are several files explaining the code.

Commission.h here is where the friend bool operator == exists and I believe I have it initialized correctly.

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
#ifndef COMMISSION_H_INCLUDED
#define COMMISSION_H_INCLUDED
using namespace std;

class Commission
{

public:
 Commission();
 Commission(int, double, double, double );
 ~Commission();

bool setbase(double);
bool setSales(double);
bool setRate(double);
bool setidNum(int);
bool  operator == (Commission& fred);

int getidNum();
double getBase();
double getSales();
double getRates();
double calcSalary();
friend bool operator ==( Commission& fred, Commission joe);

 //friend Commission operator ==(const Commission& fred, const Commission& joe);
 //friend bool operator ==(Commission fred, Commission joe);

private:

int idNumber ;       // Identification num
double bases;    // base salary
double totalSales;  // amount of sales for a period
double totalRate ;  //commission rate as a fraction

};
#endif // COMMISSION_H_INCLUDED



Commission.cpp this is the implementation of the bool operator == to avoid clustering of code I will only post the bool operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

bool  operator == (Commission& fred, Commission joe )
{
    if (fred.bases != joe.bases || fred.totalRate != joe.totalRate || fred.totalSales != joe.totalSales)
    {
                return false;
        cout << "\t Both employees are NOT equal" << endl;
    }
    else
    {
               return true;

        cout << "\t Both employees are   equal" << endl;
    }

    return 0;
}


Here is my main where the overloaded function is called.

main.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

void print(Commission& emp);

int main ()
{
    {
        //set print options
        cout << fixed << showpoint << setprecision(2);

        //variable declaration
        int    idNum = 0;   //hold input for id num
        double base  = 0.0; //hold input for base
        double sales = 0.0; //hold input for sales
        double rate  = 0.0; //holds input for rate

        //Initialize Commission object fred with base salary of $500
        //  sales of $2000 and commission rate of 10%
        Commission fred(111, 500.00, 2000.00, 0.10);
        print(fred);

        //Create Commission object joe as default
        Commission joe;
        print(joe);

        //
        //get all attribute values for joe object

        //set id num for joe
        cout << endl;
        while (true)
        {
            idNum = readInt("Enter id num: ");
            if (joe.setidNum(idNum)) break;
            cout << "\tInvalid IdNum"
                    " - must be between 0 and 999, inclusively" << endl;
        }
        print(joe);

        //set base for joe
        cout << endl;
        while (true)
        {
            base = readDbl("Enter base: ");
            if (joe.setbase(base)) break;
            cout << "\tInvalid base"
                    " - must be > 0" << endl;
        }
        print(joe);

        //set sales for joe
        cout << endl;
        while (true)
        {
            sales  = readDbl("Enter sales: ");
            if (joe.setSales(sales)) break;
            cout << "\tInvalid sales"
                    " - must be >= 0" << endl;
        }
        print(joe);

        //set rate for joe
        cout << endl;
        while (true)
        {
            rate = readDbl("Enter rate: ");
            if (joe.setRate(rate)) break;
            cout << "\tInvalid rate"
                    " - must be > 0.0 and <= 0.20" << endl;
        }

        bool operator ==(Commission& fred, Commission joe);
        print(joe);

        cout << endl;

    } //destruct joe and fred here


    //keep output window open
    cout << endl;
    return 0;
} //end main



//-------------------------------
//Name: print
//Purpose: print information for an employee on commission
//Parameters:
//  emp - employee on commission
//Returns: nothing
//-------------------------------
void print(Commission& emp)
{
    cout << "--------" << endl;
    cout << "Id Num: " << emp.getidNum()   << endl;
    cout << "Base: " << emp.getBase();
    cout << "  Sales: " << emp.getSales();
    cout << "  Rate: " << emp.getRates();
    cout << "  Salary: " << emp.calcSalary() << endl;
    cout << "--------" << endl;
}
//---------------------------------------------------


Your operator overloading method is wrong.
Try the following example

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
class A
 {
 public:
	 A(int a1=0)
	 {
		 a=a1;
	 }
	 int a;
	 bool operator==(A a2)
	 {
		 if(a==a2.a)
			 return true;
		 else
			 return false;
	 }
     
 };
 void main()
 {
	 A obj1(1),obj2(2);
	 cout<<(obj1==obj2)<<endl;
	
	 obj2.a=1;
	 cout<<(obj1==obj2)<<endl;
 }
There's nothing wrong with defining the overloaded operator outside the class, however you cannot also define it as a class member.

If you are defining it as a class member the prototype should generally be:

1
2
3
4
5
6
7
8
class A {
   // ...
   bool operator==( const A& ) const ;

   // ... or outside the class:
   friend bool operator==(const A&, const A&) ;
   // ...
};


But as I said, you cannot define it both ways. Pick one and roll with it.
main.cpp line 71: bool operator ==(Commission& fred, Commission joe);
What are you doing here? It's an forward declaration, not operator call. You just never call your overloaded operator. that is the problem.

And why you are passinf fred be reference and joe by value?
Topic archived. No new replies allowed.