c++ - 友元函数的运用
迷茫
迷茫 2017-04-17 13:53:38
0
3
469

error C2668: 'equal' : ambiguous call to overloaded function
模糊调用重载函数

#include<iostream>
using namespace std;
class DayOfYear
{
private:
    int year,month,day;
public:
    DayOfYear(){}
    DayOfYear(int x,int y){month=x;day=y;}
    int get_month(){return month;}
    int get_day(){return day;}
    void input()
    {
        cin>>month>>day;
    }
    void output()
    {
        cout<<month<<day;
    }
    friend bool equal(DayOfYear c1,DayOfYear c2);
};
bool equal(DayOfYear c1,DayOfYear c2)
{
    if((c1.get_month==c2.get_month)&&(c1.get_day==c2.get_day))
        return true;
    else
        return false;
}
int main()
{
    DayOfYear today,your_birthday(3,21);
    cout<<"Enter today's date:"<<endl;
    today.input();
    cout<<"Today's date is";
    today.output();
    cout<<"Your birthday is";
    your_birthday.output();
    if(equal(today,your_birthday))
        cout<<"Happy Birthday to You!"<<endl;
    else
        cout<<"Happy Unbirthday to You!"<<endl;
    return 0;
}

设计一个DayOfYear类,包含年、月、日等数据成员,并声明一个友元函数equal,在主函数中定义两个DayOfYear类的对象,一个用来存放生日,一个用来存放某一具体的日子,通过友元函数equal来比较这两个对象是否相等,从而来判断某一天是否为生日

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
小葫芦

The first good person has answered the question clearly. What I want to say is that your code is a bit inconsistent with the standards. Why is private placed in front of public? It is not good to put .h.cpp and main together. Variables Naming needs to follow certain rules, like int type, the general prefix is ​​i_ and so on. . .
Writing code needs to be as careful as taking care of a child

Peter_Zhu
bool equal(DayOfYear c1,DayOfYear c2)
{
    if((c1.get_month()==c2.get_month())&&(c1.get_day()==c2.get_day()))
        return true;
    else
        return false;
}

No more questions. . .

Personally I think (it has nothing to do with anyone else) it’s ugly if it’s written entirely in English, but it looks better if it’s written in Chinese in some places. For example cout<<"键入当前日期:"<<endl.

小葫芦
//建议该函数声明成:
bool equal(DayOfYear const& c1, DayOfYear const& c2)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template