C ++: LNK error: unresolved external character resulting from virtual functions

Class overview, etc. my interface!

Animal.H:

class Animal { public: virtual void walk(); } 

Animals.CPP

= EMPTY

Cow.H:

 class Cow : public Animal { public: virtual void walk(); } 

Here you should know that the walk function is taken from the class in which it is displayed on the right? (e.g. Animal ..), when I did not define the walk function, it should say that I have to define it correctly ...?

Cow.CPP:

 void Cow::walk() { //do something specific for cow } 

SomeOtherClass.H

 namespace SomeNamespace { void LetAnimalWalk(); } 

SomeOtherClass.CPP

 Cow myCow; namespace SomeNamespace { void LetAnimalWalk() { myCow.walk(); } } 

Should this work correctly? ... I mean the namespace, "class :: ..."? and how do I inherit and use the interface?

Because in this way I get with EVERY FUNCTION made from the interface, so every virtual function gives me the following error:

 SomeOtherClass.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Cow::Walk (...etc etc...) referenced in function "void __cdecl SomeNamespace::LetAnimalWalk() (...etc etc...) 

Does anyone know what I'm doing wrong, what I really find is that it means that I did not declare the function on the right (somewhere in Cow.cpp ??)

Thanks in advance guys

+7
source share
4 answers
 class Animal { public: virtual void walk(); } 

You need to define this function or make it pure virtual.

+9
source

If you have a virtual function that you do not want to define in the base class, you need to make it abstract, this is done using the following syntax:

 class Animal { public: virtual void walk() = 0; } 

Without this, you will receive an error message if it is not defined, and with this you will receive an error message if any derived class does not define it.

+4
source

either make the function in the base class pure virtual:

 class Animal { public: virtual void walk() = 0; } 

or define a trivial implementation:

 void Animal::walk() { return; // could also throw an exception here } 
+2
source

Animal * animal = new Cow (); animal-> walk ();

The myCow cow is NOT working explicitly!

+1
source

All Articles