| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1700 人关注过本帖
标题:基类中函数调用派生类对象做参数,编译出错,是为什么?
取消只看楼主 加入收藏
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:11 
基类中函数调用派生类对象做参数,编译出错,是为什么?
如: Time 是基类 里面有个virtual void add(Time_12hours&t1)=0;函数,
Time_12hours是Time的一个派生类,编译时提示identifier 'Time_12hours',
我也实验过,肯定是函数参数的问题,不知道怎么解决?
2013-06-20 09:33
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”
};

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:{hour=hour+12;}
        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;}
        }
    }
}

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)
 {
     switch(interval)
    {
        case "PM":{hour=hour+12;}
        case "AM":
        {
            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_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)
{
    switch(interval)
    {
        case "PM":{hour=hour+12;}
        case "AM":
        {
        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;}
        }
    }
}

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;
}









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_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:{hour=hour+12;}
        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:{hour=hour+12;}
        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:{hour=hour+12;}
        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=&t1;}break;
        case 122:{t2.set_interval_2();t2.set_date();time2=&t1;}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 10:54
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
我知道问题出在哪:当在先建立的类中引用后建立的类对象当函数参数时,编译会出错。  这怎么解决?  比如我在Time_12hours 中>重载时引用Time_24hours类对象当参数  并定义,这时候就会出错。
2013-06-20 10:56
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
回复 2楼 rjsp
我贴好了
2013-06-20 11:02
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
回复 6楼 rjsp
感谢! 我刚学不久,不好意思,顺便再问一个问题:一般写c++的时候,有多个类,标准的格式是:所有类的声明都写在前面,所有类中的函数的定义都统一写在后面是吗?
2013-06-20 11:30
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
回复 6楼 rjsp
版主,那个问题解决了,麻烦你再看看我的main函数 我在用基类指针进行判断 > < ==时候又出错了,error C2679: binary '<' : no operator defined which takes a right-hand operand of type 'class Time' (or there is no acceptable conversion)  


谢谢。。。。。
2013-06-20 11:42
hebinjie33
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2012-12-20
收藏
得分:0 
回复 6楼 rjsp
我试了一下不是switch 的问题,我把switch删去改成:int main()
{
    Time *time1,*time2;
    Time_12hours t1,t2;
    Time_24hours t3,t4;

    t1.set_date();
    time1=&t1;
    t3.set_date();
    time2=&t3;


        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;

}


问题依旧,报错说time1 和time2 不属于Time基类,可是我用了虚函数系统应该会识别啊。
2013-06-20 11:51
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
快速回复:基类中函数调用派生类对象做参数,编译出错,是为什么?
数据加载中...
 
   



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

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