简体   繁体   中英

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

Error: conflicting return type specified for 'virtual int Dog::getClass()', where it says getClass(){ cout << "I'm a dog" << endl; }

Also, it says:

Class 'Dog' has virtual method 'getClass' but non-virtual destructor

#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:

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

When you declare it with no return type, the compiler sets the return type to 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++. 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). It has been mentioned in the standard - §7/7 footnote 78, and §7.1.5/2 footnote 80: implicit 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 ?

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++
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.

You are missing your type-specifier which is illegal in modern C++.

Use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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