| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1700 人关注过本帖
标题:基类中函数调用派生类对象做参数,编译出错,是为什么?
只看楼主 加入收藏
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
#include<iostream>
#include<string>
using namespace std;
class Time_12hours;
class Time_24hours;
class Time
{      
public:
    virtual bool operator > (Time_12hours&)=0;
    virtual bool operator > (Time_24hours&)=0;
    virtual bool operator ==(Time_12hours&)=0;
    virtual bool operator ==(Time_24hours&)=0;
    virtual bool operator <(Time_12hours&)=0;
    virtual bool operator <(Time_24hours&)=0;
    virtual void display()=0;
    int second;
    int minute;
    int hour;
};





class Time_12hours: public Time
{
public:
         Time_12hours():type("12-hours-time"),interval("AM"){}
         void set_date();
         void set_interval_1(){interval="AM";}            //用于主函数里的判断,给interval赋值
         void set_interval_2(){interval="PM";}            //用于主函数里的判断,给interval赋值
         virtual bool operator>(Time_12hours&);
         virtual bool operator>(Time_24hours&);
         virtual bool operator==(Time_12hours&);
         virtual bool operator==(Time_24hours&);
         virtual bool operator<(Time_12hours&);
         virtual bool operator<(Time_24hours&);
         virtual void display();
         string interval;            //标识为AM或者PM,interval=”AM”或interval=”PM”
private:   
         string type;                //标识为12进制时间,type=”12-hours-time”
};


class Time_24hours: public Time
{
public:   
    Time_24hours():type("24-hours-time"){}
    void set_date();
    virtual bool operator>(Time_12hours&);
    virtual bool operator>(Time_24hours&);
    virtual bool operator==(Time_12hours&);
    virtual bool operator==(Time_24hours&);
    virtual bool operator<(Time_12hours&);
    virtual bool operator<(Time_24hours&);
    virtual void display();
private:   
    string type;            //标识为24进制时间,type=”24-hours-time”
};






void Time_12hours::set_date()
{
    cin>>hour>>minute>>second;
}

bool Time_12hours::operator>(Time_12hours& t1)
{

    if(interval==t1.interval)
    {
        if(hour>t1.hour){return true;}
        else if(hour<t1.hour){return false;}
        else if(minute>t1.minute){return true;}
        else if(minute<t1.minute){return false;}
        else if(second>t1.second){return true;}
        else if(second>=t1.second){return false;}
    }
    else if(interval=="PM"){return true;}
    else {return false;}
}

bool Time_12hours::operator>(Time_24hours& t2)
{
    int j;
    if(interval=="AM")j=0;
    else j=1;
    switch(j)
    {
        case 0:        
        {
        if(hour+12>t2.hour){return true;}
        else if(hour+12<t2.hour){return false;}
        else if(minute>t2.minute){return true;}
        else if(minute<t2.minute){return false;}
        else if(second>t2.second){return true;}
        else if(second>=t2.second){return false;}
        }break;
        case 1:
        {
        if(hour>t2.hour){return true;}
        else if(hour<t2.hour){return false;}
        else if(minute>t2.minute){return true;}
        else if(minute<t2.minute){return false;}
        else if(second>t2.second){return true;}
        else if(second>=t2.second){return false;}
        }break;
    }
}

bool Time_12hours::operator==(Time_12hours& t1)
{
    if(interval==t1.interval)
    {
        if(hour==t1.hour)
        {
            if(minute==t1.minute)
            {
                if(second==t1.second)
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }
    else return false;
}

 bool Time_12hours::operator==(Time_24hours& t2)
 {
     int j;
         if(interval=="PM")j=1;
         else j=0;
     switch(j)
    {
        case 1:
        {
            if(hour+12==t2.hour)
            {
                if(minute==t2.minute)
                {
                    if(second==t2.second)
                    {
                        return true;
                    }
                    else return false;
                }
                else return false;
            }
            else return false;
        }
        case 0:
        {
            if(hour==t2.hour)
            {
                if(minute==t2.minute)
                {
                    if(second==t2.second)
                    {
                        return true;
                    }
                    else return false;
                }
                else return false;
            }
            else return false;
        }
     }
 }

bool Time_12hours::operator<(Time_12hours& t1)
{
    if(interval==t1.interval)
    {
        if(hour<t1.hour){return true;}
        else if(hour>t1.hour){return false;}
        else if(minute<t1.minute){return true;}
        else if(minute>t1.minute){return false;}
        else if(second<t1.second){return true;}
        else if(second>=t1.second){return false;}
    }
    else if(interval=="AM"){return true;}
    else {return false;}
}

bool Time_12hours::operator<(Time_24hours& t2)
{
    int j;
         if(interval=="PM")j=1;
         else j=0;
    switch(j)
    {
        case 1:{
        if(hour+12<t2.hour){return true;}
        else if(hour+12>t2.hour){return false;}
        else if(minute<t2.minute){return true;}
        else if(minute>t2.minute){return false;}
        else if(second<t2.second){return true;}
        else if(second>=t2.second){return false;}
        }
        case 0:
        {
        if(hour<t2.hour){return true;}
        else if(hour>t2.hour){return false;}
        else if(minute<t2.minute){return true;}
        else if(minute>t2.minute){return false;}
        else if(second<t2.second){return true;}
        else if(second>=t2.second){return false;}
        }
    }
}

void Time_12hours::display()
{
    cout<<interval<<" ";
    if(hour<10)
        cout<<"0"<<hour<<":";
    else
        cout<<hour<<":";
    if(minute<10)
        cout<<"0"<<minute<<":";
    else
        cout<<minute<<":";
    if(second<10)
        cout<<"0"<<second<<endl;
    else
        cout<<second<<endl;
}









void Time_24hours::set_date()
{   
    cin>>hour>>minute>>second;
}

bool Time_24hours::operator>(Time_12hours& t1)
{
    int j;
    if(t1.interval=="PM")j=1;
    else j=0;
    switch(j)
    {
        case 1:{
        if(hour>t1.hour+12){return true;}
        else if(hour<t1.hour+12){return false;}
        else if(minute>t1.minute){return true;}
        else if(minute<t1.minute){return false;}
        else if(second>t1.second){return true;}
        else if(second>=t1.second){return false;}
        }
        case 0:
        {
        if(hour>t1.hour){return true;}
        else if(hour<t1.hour){return false;}
        else if(minute>t1.minute){return true;}
        else if(minute<t1.minute){return false;}
        else if(second>t1.second){return true;}
        else if(second>=t1.second){return false;}
        }
    }
}
   
bool Time_24hours::operator>(Time_24hours& t2)
{
        if(hour>t2.hour){return true;}
        else if(hour<t2.hour){return false;}
        else if(minute>t2.minute){return true;}
        else if(minute<t2.minute){return false;}
        else if(second>t2.second){return true;}
        else if(second>=t2.second){return false;}
}


bool Time_24hours::operator==(Time_12hours& t1)
{
    int j;
    if(t1.interval=="PM")j=1;
    else j=0;
        
         switch(j)
    {
        case 1:
        {
            if(hour==t1.hour+12)
            {
                if(minute==t1.minute)
                {
                    if(second==t1.second)
                    {
                        return true;
                    }
                    else return false;
                }
                else return false;
            }
            else return false;
        }
     
        case 0:
        {
            if(hour==t1.hour)
            {
                if(minute==t1.minute)
                {
                    if(second==t1.second)
                    {
                        return true;
                    }
                    else return false;
                }
                else return false;
            }
            else return false;
        }
    }
}
   

 bool Time_24hours::operator==(Time_24hours& t2)
 {
        if(hour==t2.hour)
        {
            if(minute==t2.minute)
            {
                if(second==t2.second)
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }
        else return false;
 }


bool Time_24hours::operator<(Time_12hours& t1)
{
    int j;
    if(t1.interval=="PM")j=1;
    else j=0;
        switch(j)
    {
        case 1:
        {
        if(hour<t1.hour+12){return true;}
        else if(hour>t1.hour+12){return false;}
        else if(minute<t1.minute){return true;}
        else if(minute>t1.minute){return false;}
        else if(second<t1.second){return true;}
        else if(second>=t1.second){return false;}
        }
        case 0:
        {
        if(hour<t1.hour){return true;}
        else if(hour>t1.hour){return false;}
        else if(minute<t1.minute){return true;}
        else if(minute>t1.minute){return false;}
        else if(second<t1.second){return true;}
        else if(second>=t1.second){return false;}
        }
    }
   
}

bool Time_24hours::operator<(Time_24hours& t2)
{
    {
        if(hour<t2.hour){return true;}
        else if(hour>t2.hour){return false;}
        else if(minute<t2.minute){return true;}
        else if(minute>t2.minute){return false;}
        else if(second<t2.second){return true;}
        else if(second>=t2.second){return false;}
    }
}

void Time_24hours::display()
{
    if(hour<10)
        cout<<"0"<<hour<<":";
    else
        cout<<hour<<":";
    if(minute<10)
        cout<<"0"<<minute<<":";
    else
        cout<<minute<<":";
    if(second<10)
        cout<<"0"<<second<<endl;
    else
        cout<<second<<endl;
}











int main()
{
    Time *time1,*time2;
    Time_12hours t1,t2;
    Time_24hours t3,t4;
    int n;  //需要比较的时间组数
    int j;  //选择时间种类

    cin>>n;  //输入需要比较的时间组数

    for(;n>0;n--)
    {
        cin>>j;
        switch(j)
        {
        case 121:{t1.set_interval_1();t1.set_date();time1=&t1;}break;
        case 122:{t1.set_interval_2();t1.set_date();time1=&t1;}break;
        case 24:{t3.set_date();time1=&t3;}break;
        }

        cin>>j;
        
        switch(j)
        {
        case 121:{t2.set_interval_1();t2.set_date();time2=&t2;}break;
        case 122:{t2.set_interval_2();t2.set_date();time2=&t2;}break;
        case 24:{t4.set_date();time1=&t4;}break;
        }
        if((*time1)<(*time2))
        {
        cout<<"time1";
        time1->display();
        cout<<"<";
        time2->display();
        }

        else if(*time1>*time2)
        {
        cout<<"time1";
        time1->display();
        cout<<">";
        time2->display();
        }
        else if(*time1==*time2)
        {
        cout<<"time1";
        time1->display();
        cout<<"==";
        time2->display();
        }
        else
        {
        cout<<"There is something wrong with the input."
            <<"Please check it!"<<endl;
        }
    }
    return 0;

}
  


找不到什么无关的。。。。。这就是完整代码了。。。
2013-06-20 12:18
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
问题会不会在用动态多态性的时候出了问题。系统没法识别多态性。
2013-06-20 12:20
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
我试过了,重载也没问题,问题是不是:3个类中的重载“>”因为所在类的不同 所以参数也不同,导致动态多态性调用失败。
2013-06-20 12:26
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
回复 11楼 hebinjie33
比如 if((*time1)<(*time2))
两者都是 Time 类型,编译器不知道你想用
    virtual bool operator <(Time_12hours&)=0;
还是想用
    virtual bool operator <(Time_24hours&)=0;
2013-06-20 13:07
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
回复 14楼 rjsp
可是我已经用switch语句把time2赋值为time_12hours或者time_24hours类型了,编译器应该会知道这是重载了吧。

再者,我刚写了一下,明确把time2赋值为Time_12hours类型 time1也赋值为Time_12hours  也不行。

虽然time1是基类的指针,可是我定义了虚函数,应该会进行动态多态性的调用啊。
2013-06-20 13:14
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
我认为是> < == 这三个运算符在不同类中写的时候,类本身被默认为一个参数,导致不同类中的运算符参数类型不同,无法形成虚函数。

有什么解决办法吗
2013-06-20 13:21
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
回复 15楼 hebinjie33
你搞不清 override 和 overload 呀
2013-06-20 13:21
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
回复 16楼 hebinjie33
你说的东西虽然神奇,但肯定不是C++。
你还是多看看C++的基础书籍吧
2013-06-20 13:22
快速回复:基类中函数调用派生类对象做参数,编译出错,是为什么?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015539 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved