0

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;
}
2
  • 1
    As for virtual destructor see: When to use virtual destructors? since your class has a virtual function it should also have a virtual destructor. Jan 15, 2016 at 2:49
  • 1
    You should add a virtual destructor if you destruct your class in a polymorphic context. If that definition does not ring a bell with you yet, then just always add one. Note that in the example of @SalahSalah there is no destructor call at all.
    – dascandy
    Jan 15, 2016 at 5:03

3 Answers 3

6

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.

1
  • 4
    Default int return type isn't even supported by the C++ standard (it's deprecated in C) and should give a warning/error about a missing (return) type in every modern compiler. Jan 15, 2016 at 2:53
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.