4

I am trying to build a zoo for practicing c++ and oop. I have made 2 classes, Animal class (the base class) and Bear class (the derived class). I want to have 2 virtual functions in Animal that I will override in Bear but CLION tells me that 'Function Walk did not decleared in class Bear'.

What do I need to change?

This is the base class (Animal) header:

class Animal { public: Animal(); Animal(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment); virtual void Talk() = 0; virtual void Walk(); int CageNum; string FoodType; string Gender; bool NeedTreatment; string Name; }; 

CPP:

Animal::Animal() {}; Animal::Animal(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment) : Name(Name), CageNum(CageNum), FoodType(FoodType), Gender(Gender), NeedTreatment(NeedTreatment){}; 

This is the derived class (Bear) header:

#include "Animal.h" class Bear : public Animal{ protected: string FurColor; public: Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor); }; 

and this is the CPP:

#include "Bear.h" Bear::Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor) : Animal(Name, CageNum, FoodType, Gender, NeedTreatment),FurColor(FurColor) {}; void Bear::Walk() { cout << "Bear Moves"; } void Animal::Talk() { "Bear Noise"; } 
1
  • 4
    What's the real error message? Commented Jul 15, 2018 at 7:55

1 Answer 1

3

If you define a function like

void Bear::Walk() { cout << "Bear Moves"; } 

its declaration must appear in the class definition:

class Bear : public Animal{ protected: string FurColor; public: Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor); void Walk() override; // <<<<<<<<<<<<<<<<<< }; 

Alternatively you can omit the definition, and the base class member definition Animal::Walk() will be used.


Also

void Animal::Talk() { "Bear Noise"; } 

is wrong (or at last doesn't do what is intended).

A pure virtual function like Talk() from the base class, must have a declaration and definition in the Bear class, unless the class is intentionally left abstract.

class Bear : public Animal{ protected: string FurColor; public: Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor); void Talk() override; // <<<<<<<<<<<<<<<<<< void Walk() override; }; 

and the definition

void Bear::Talk() { "Bear Noise"; } 
Sign up to request clarification or add additional context in comments.

11 Comments

I want to declare virtual method at the animal class and implenent it at the Bear class
@YoavLinder As mentioned, what has a definition must also have a declaration at the same scope.
Then what's the reason to use virtual function? I can juat declare these functions in all the derived classes
@FrancisCugler *"This ensures that the derived methods class will be called over the base class version..." It does no such thing. override is a specifier to the compiler to check if the function is virtual and that it is overriding a virtual function from the base class, otherwise emit an error. In other words, it checks at compile-time to make sure you are actually overriding a base class function and has nothing to do with run-time calls. See: en.cppreference.com/w/cpp/language/override
@FrancisCugler Almost. :P Again, it only has to do with declarations, not whether or not a function is called. "...has been declared" would be a better choice of words. :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.