Multi-inheritance — struggle

Posted: July 13, 2013 in Programming
Tags: , , ,

Tried to resolve the OOP344 exercise with multi-inheritance (using templates) and after the compilation got this error.
Could anyone help me to figure out the error reason?!
Link to the initial files on github.

  main.cpp
main.obj : error LNK2001: unresolved external symbol "private: static unsigned int Base::count" (?count@?$Base@ViSpeaker@@@@0IA)
main.obj : error LNK2001: unresolved external symbol "private: static unsigned int Base::count" (?count@?$Base@ViBell@@@@0IA)
Project2.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Basically, my headers:
File — Base.h

#ifndef __BASE__
#define __BASE__
#include "iBase.h"

template <class T = iBase>
class Base : public T
   {
   private:
      static unsigned int count;

      unsigned int id;
   protected:
   public:
      Base() : id(count++) {}
      unsigned int getID() {return id;}
   };

#endif

File — Bell.h

#ifndef __BELL__
#define __BELL__
#include "iBell.h"
#include "SoundMaker.h"

template <class T = iBell>
class Bell : public SoundMaker<T>
   {
   private:
   protected:
   public:
      Bell() : SoundMaker<T>() {sound = "Ding";}
      void switchHammer()
         {
         if(sound == "Ding")
            sound = "Dong";
         else
            sound = "Ding";
         }
   };

#endif

File — SoundMaker.h

#ifndef __SOUND_MAKER__
#define __SOUND_MAKER__
#include
#include
#include "iSoundMaker.h"
#include "Base.h"

template <class T = iSoundMaker>
class SoundMaker : public Base<T>
   {
   private:
   protected:
      std::string sound;
   public:
      SoundMaker() : Base<T>(), sound("Bang") {}
      void playSound()
	  {
		  std::cout << sound << "\n";
	  }
   };

#endif

File — Speaker.h

#ifndef __SPEAKER__
#define __SPEAKER__
#include "iSpeaker.h"
#include "SoundMaker.h"

template <class T = iSpeaker>
class Speaker : public SoundMaker<T>
   {
   private:
   protected:
   public:
      Speaker() : SoundMaker<>() {}
      void increaseVolume()
         {
         this->sound += "!";
         }
   };

#endif

And finally my main():

#include "Speaker.h"
#include "Bell.h"

template<class T>
unsigned int Base::count = 0; //static initialization WARNING!
int main()
   {
   Speaker<> a;
   Speaker<> b;

   Bell<> c;

   a.playSound();
   a.increaseVolume();

   b.increaseVolume();
   b.playSound();

   c.playSound();
   c.switchHammer();
   c.playSound();

   std::cout << a.getID() << " " << b.getID() << " " << c.getID();
   return 0;
   }
Advertisements
Comments
  1. Joseph Hughes says:

    Your solution for the most part is looking good, but you have to remember to initialize a static variable in the CPP file that the .h file will be included from!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s