鱼C论坛

 找回密码
 立即注册
查看: 1436|回复: 3

[已解决]形参为基类对象传递派生类实例时报错

[复制链接]
发表于 2019-3-18 17:40:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
RT,代码很简单,有大佬细讲下吗

基类:
class Pet{
public:
        Pet(string type){
                this->type = type;          
        }
        Pet();
        string getPetType(){
                return this->type;
        }
private: 
        string type;
};

派生类:
class Cat : public Pet{
public:
        Cat():Pet("cat"){
        }
};

调用的函数:
        void add(Pet item){
                if (item.getPetType()=="cat")
                {
                        CatQueue.push(peterPetQueue(item,count1++));
                }
                else{
                        DogQueue.push(peterPetQueue(item,count1++));
                }
        }

报错:

                               
登录/注册后可看大图
最佳答案
2019-3-18 17:46:44
public:
        Pet(string type){
                this->type = type;          
        }
        Pet();
        string getPetType(){
                return this->type;
        }
你声明了pet();但没有给出定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-18 17:46:44 | 显示全部楼层    本楼为最佳答案   
public:
        Pet(string type){
                this->type = type;          
        }
        Pet();
        string getPetType(){
                return this->type;
        }
你声明了pet();但没有给出定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-18 17:55:00 | 显示全部楼层
Croper 发表于 2019-3-18 17:46
你声明了pet();但没有给出定义

谢谢了,请问为什么给出定义了就可以呢qwq
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-18 18:09:26 | 显示全部楼层
本帖最后由 Croper 于 2019-3-18 18:13 编辑

一般来说是你什么地方隐式调用了pet(),不给完整代码我也不知道是哪儿
比如你CatAndDog是什么类
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 12:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表