How can I convert an operator in a C++ class to work as a friend function?

Hello,

I am trying to rewrite some example code as a friend function instead of an operator function. I have tried a new declaration in the prototype and beginning of the function. But get an error of

"Declaration of operator== as non function"

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
  #include <iostream>
using namespace std;

// class declaration section
class Date
{
    private:
        int month;
        int day;
        int year;

    public:
        Date(int = 7, int = 4, int = 2005); // constructor
        friend bool operator==(Date &, Date &); // declare the operator== function
};

// class implementation section
Date::Date(int mm, int dd, int yyyy)
{
    month = mm;
    day = dd;
    year = yyyy;
}

bool operator==(&date1, &date2)
{
    if(day == date2.day && month == date2.month && year == date2.year)
        return true;
    else
        return false;
}

int main()
{

Date a(4,1,2007), b(12,18,2008), c(4,1,2007); // declare 3 objects

    if (a == b)
        cout << "Dates a and b are the same." << endl;
    else
        cout << "Dates a and b are not the same." << endl;
    if (a == c)
        cout << "Dates a and c are the same." << endl;
    else
        cout << "Dates a and c are not the same." << endl;

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Date
{
    private:
        int month;
        int day;
        int year;

    public:
        Date( int = 7, int = 4, int = 2005 );

        // ...

    friend bool operator==( const Date&, const Date& );
};

bool operator==( const Date& a, const Date& b )
{
    return a.month == b.month && a.day == b.day && a.year == b.year ;
}
1
2
3
4
5
6
7
bool operator==(Date& date1, Date& date2)
{
    if((date1.day == date2.day) && (date1.month == date2.month) && (date1.year == date2.year))
        return true;
    else
        return false;
}

suggest const qualification as well
Last edited on
It seems I've been too slow... :-)
May I humbly add some clarifications about what I think were the errors?

friend bool operator==(Date &, Date &);

bool operator==(&date1, &date2)

The first declaration of operator==() is correct, but the second isn’t: “&date1” and “&date2” aren’t types, but instances of a type (instances of “Date”).

Also:
if(day == date2.day && month == date2.month && year == date2.year)
Once you’ve moved your operator==() outside the declaration of “Date”, it doesn’t have any more a “this”, so it cannot know which “day” or “month” you are referring to: you must always specify that.

Also: operator==() is not supposed to modify your Date(s), so you can pass them to it as constants.
May I humbly add some clarifications ...
very modest but in fact your clarifications were excellent!
:) :)
Thanks a lot, gunnerfunner, I really appreciate your comment.
Aloha all!

So the final result is

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
#include <iostream>
using namespace std;

// class declaration section
class Date
{
    private:
        int month;
        int day;
        int year;

    public:
        Date(int = 7, int = 4, int = 2005); // constructor


    friend bool operator==(Date &, Date &); // declare the operator== function
};

// class implementation section
Date::Date(int mm, int dd, int yyyy)
{
    month = mm;
    day = dd;
    year = yyyy;
}

bool operator==(Date &date1, Date &date2)
{
    if(date1.day == date2.day && date1.month == date2.month && date1.year == date2.year)
        return true;
    else
        return false;
}

int main()
{

Date a(4,1,2007), b(12,18,2008), c(4,1,2007); // declare 3 objects

    if (a == b)
        cout << "Dates a and b are the same." << endl;
    else
        cout << "Dates a and b are not the same." << endl;
    if (a == c)
        cout << "Dates a and c are the same." << endl;
    else
        cout << "Dates a and c are not the same." << endl;

return 0;

}


Which works a treat. So i have to forgotten to declare the instances of date and compared the days, months and years from date objects that weren't initially referred to.
Again to you, thanks again. I am just learning this from and old university textbook.
Topic archived. No new replies allowed.