C++: error LNK: unresolved external symbol, resulting from virtual functions

Sebastiaan van Dorst picture Sebastiaan van Dorst · Mar 7, 2013 · Viewed 19.6k times · Source

Overview of classes etc of my interface!

Animal.H:

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

Animals.CPP

=EMPTY

Cow.H:

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

Here it should outomatically know the function walk is taken from the class where it's derived from right? (e.a. Animal..) when i wouldnt define the function walk, it should say i should define it right...?

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

This should work right?... i mean, the namespace, the "Class::..." things? and the way i inherit and use the interface?

Because this way i get with EVERY FUNCTION i 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 do find mostly is that it means i've not declared a function right (somewhere in Cow.cpp??)

Thanks in advance guys

Answer

Barış Uşaklı picture Barış Uşaklı · Mar 7, 2013
class Animal
{
public:
   virtual void walk();
}

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