多态性测试不起作用

时间:2012-08-29 11:09:54

标签: c++ polymorphism

以下代码无法编译,但我无法理解错误:

#include <iostream>

class FamilyMember {

    int age;
    public:
    virtual int myage () = 0;
};


class Grandfather: public FamilyMember {

    int age;
    public:
    Grandfather (): age(60) {
        std::cout << "Im grandpa" << std::endl;
    }
    ~Grandfather () {
        std::cout << "Oh no! Grandpa is dead!" << std::endl;
    }
    virtual int myage () const {
        return age;
    }
};


class Father: public Grandfather {

    int age;
    public:
    Father (): age(40) {
        std::cout << "Im papa" << std::endl;
    }
    ~Father () {
        std::cout << "Papa is gone, noooooo!" << std::endl;
    }
    virtual int myage () const {
        return age;
    }
};


class Son: public Father {

    int age;
    public:
    Son (): age(20) {
        std::cout << "Im the kid" << std::endl;
    }
    ~Son () {
        std::cout << "Son is dead? He was so young!" << std::endl;
    }
    int myage () const {
        return age;
    }
};

int main () {

    Grandfather G;
    Father F;
    Son S;

   return 0;
}

以下是我得到的错误(我将代码减少到打破它的最小数量,因此行号不匹配)。

main.cc:535: error: cannot declare variable ‘G’ to be of abstract type ‘Grandfather’
main.cc:161: note:   because the following virtual functions are pure within ‘Grandfather’:
main.cc:157: note:  virtual int FamilyMember::myage()
main.cc:536: error: cannot declare variable ‘F’ to be of abstract type ‘Father’
main.cc:177: note:   because the following virtual functions are pure within ‘Father’:
main.cc:157: note:  virtual int FamilyMember::myage()
main.cc:537: error: cannot declare variable ‘S’ to be of abstract type ‘Son’
main.cc:193: note:   because the following virtual functions are pure within ‘Son’:
main.cc:157: note:  virtual int FamilyMember::myage()
make: *** [main.o] Error 1
Compilation failed.

6 个答案:

答案 0 :(得分:6)

不同的签名。

virtual int myage () = 0;

在儿童班。

virtual int myage () const

也可以pure-virtual myage const,或者在孩子身上制作non-const此功能。

答案 1 :(得分:1)

除了在所有其他答案中所说的内容之外,C ++ 11还提供了特殊标识的override,它可以在编译时指出错误:

class Grandfather: public FamilyMember {

    // as before ...

    virtual int myage () const override {  // Error! Not overriding.
        return age;
    }
};

答案 2 :(得分:0)

您没有正确覆盖myage

virtual int myage () = 0;

不同

virtual int myage () const

方法上的const将使其成为不同的签名,因此采用不同的方法

答案 3 :(得分:0)

这在基础

中被声明为纯虚拟
 virtual int myage () = 0; 

但在派生类中,函数原型是

 virtual int myage () const 

这是一个不同的函数,因此基类中的非const版本没有被覆盖,编译错误告诉你这个。

答案 4 :(得分:0)

“FamilyMember”和“祖父”课程中的功能myage有不同的签名。

virtual int myage()= 0; //在FamilyMember中

virtual int myage()const //在祖父

尝试更改类FamilyMember的定义,如下所示     class FamilyMember {

int age;
public:
virtual int myage () const = 0;
};

答案 5 :(得分:0)

另外,如果不给每个子类赋予age属性更合理,只需使用超类具有的属性。

class FamilyMember
{
    protected:
        int age;
    public:
        virtual int myage ()  const = 0;
        FamilyMember(int a) : age(a) {}
};

class Grandfather: public FamilyMember
{
    public:
        Grandfather (): FamilyMember(60) {}
        ~Grandfather () {}
        virtual int myage () const { return FamilyMember::age; }
};