0

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

#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.
4

6 回答 6

6

不同的签名。

virtual int myage () = 0;

在儿童班。

virtual int myage () const

也制作pure-virtual myage const,或non-const在孩子中制作此功能。

于 2012-08-29T11:11:33.533 回答
1

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

class Grandfather: public FamilyMember {

    // as before ...

    virtual int myage () const override {  // Error! Not overriding.
        return age;
    }
};
于 2012-08-29T11:21:27.233 回答
0

您没有myage正确覆盖。

virtual int myage () = 0;

不是一回事

virtual int myage () const

on 方法将const使其成为不同的签名,因此是不同的方法

于 2012-08-29T11:12:20.077 回答
0

这在基础中声明为纯虚拟

 virtual int myage () = 0; 

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

 virtual int myage () const 

这是一个不同的函数,因此您在基类中的非常量版本没有被覆盖,编译器错误告诉您这一点。

于 2012-08-29T11:12:50.787 回答
0

“FamilyMember”和“Grandfather”类中的函数 myage 具有不同的签名。

虚拟 int myage () = 0; // 在家庭成员中

virtual int myage () const // 在祖父中

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

int age;
public:
virtual int myage () const = 0;
};
于 2012-08-29T11:14:39.963 回答
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; }
};
于 2012-08-29T11:20:55.453 回答