简体   繁体   English

覆盖'virtual void'c ++错误

[英]overriding 'virtual void ' c++ error

The following code gives me an error. 以下代码给我一个错误。

Error: overriding 'virtual void Animal::getClass()', where it says virtual void getClass() { cout << "I'm an animal" << endl; 错误:覆盖了'virtual void Animal :: getClass()',其中说virtual void getClass(){cout <<“我是动物” << endl; } }

Error: conflicting return type specified for 'virtual int Dog::getClass()', where it says getClass(){ cout << "I'm a dog" << endl; 错误:为'virtual int Dog :: getClass()'指定的返回类型冲突,它说getClass(){cout <<“我是狗” << endl; } }

Also, it says: 另外,它说:

Class 'Dog' has virtual method 'getClass' but non-virtual destructor “狗”类具有虚拟方法“ getClass”,但非虚拟析构函数

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include<sstream>
#include <stdlib.h>     // srand, rand
#include <stdio.h>

using namespace std;

class Animal{

public:
    void getFamily(){ cout << "We are animals " << endl;}

    virtual void getClass() { cout << "I'm an animal" << endl; }
};

class Dog : public Animal{

public:
    getClass(){ cout << "I'm a dog" << endl; }

};

void whatClassAreYou(Animal *animal){

    animal -> getClass();

}

int main(){

    Animal *animal = new Animal;
    Dog *dog = new Dog;

    animal->getClass();
    dog->getClass();

    whatClassAreYou(animal);
    whatClassAreYou(dog);


    return 0;
}

Change the definition inside class Dog to: 将类Dog中的定义更改为:

void getClass() { /* ... */ }

When you declare it with no return type, the compiler sets the return type to int. 当您声明没有返回类型时,编译器会将返回类型设置为int。 This yields an error because the overriden method has to have the same return type as the base class method it overrides. 这会产生错误,因为重写的方法必须具有与其覆盖的基类方法相同的返回类型。

You are trying to declare function with no return type which was allowed in older version of C++. 您正在尝试声明不具有C ++较早版本允许的返回类型的函数。 But the ISO C++ standard doesn't allow this (although some compilers may still allow, I guess like Codegear C++Builder 2007 that shows no error or warning at all). 但是,ISO C ++标准不允许这样做(尽管某些编译器可能仍然允许,我想像Codegear C ++ Builder 2007一样根本不显示错误或警告)。 It has been mentioned in the standard - §7/7 footnote 78, and §7.1.5/2 footnote 80: implicit int banned . 在标准§7/ 7脚注78和§7.1.5/ 2脚注80: 隐式int banned中已提到它。

Here is a reason why it was discarded: 这是它被丢弃的原因:

 void HypotheticalFunction(const Type);

In this function, what would be the argument type - const argument of type Type or argument of type const int with a name Type ? 在此函数中,类型为Type的参数类型-const参数或名称为Type的类型为const int的参数是什么?

Here is a better version of how you could define your class: 这是定义类的一个更好的版本:

 #include <iostream>
 #include <vector>
 #include <string>
 #include <fstream>
 #include<sstream>
 #include <stdlib.h>     // srand, rand
 #include <stdio.h>

using namespace std;

class Animal{

public:
void getFamily(){ cout << "We are animals " << endl;}

virtual void getClass() { cout << "I'm an animal" << endl; }
};

class Dog : public Animal{

public:
virtual void getClass(){ cout << "I'm a dog" << endl; }

};

void whatClassAreYou(Animal *animal){

animal -> getClass();

}

int main(){

Animal *animal = new Animal;
Dog *dog = new Dog;

animal->getClass();  // I'm an animal
dog->getClass();     // I'm a dog

Animal *animal1 = new Dog;
animal1->getClass(); // I'm a dog

whatClassAreYou(animal1); // I'm a dog

whatClassAreYou(animal); // I'm an animal

return 0;
}

§ C.1.6 Change: Banning implicit int In C++ §C.1.6更改:在C ++中禁止隐式int
a decl-specifier-seq must contain a type-specifier , unless it is followed by a declarator for a constructor, a destructor, or a conversion function. decl-specifier-seq必须包含一个type-specifier ,除非它后面是构造函数,析构函数或转换函数的声明type-specifier

You are missing your type-specifier which is illegal in modern C++. 您缺少type-specifier ,这在现代C ++中是非法的。

Use: 采用:

void getClass(){ cout << "I'm a dog" << endl; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM