Go Down

Topic: Multiple inheritance problem (Read 317 times) previous topic - next topic

mistergreen

Mar 22, 2013, 01:01 am Last Edit: Mar 22, 2013, 02:16 am by mistergreen Reason: 1
I have 2 base classes

Code: [Select]

class BaseDevice
{
 public:
   virtual void read() = 0;
   virtual void debugThis();
};

class Debug
{
 public:
   void debugThis() { Serial.print("debug!"); };
 
 
};



Child class.
Code: [Select]

class Morse : public BaseDevice, public Debug
{
 public:
   Morse(int pin) { _pin = pin; }
   void read() { Serial.print("read"); }
 private:
   int _pin;
};




Code: [Select]

BaseDevice *morse;
morse = new Morse(13);
morse->debugThis();


I keep getting test_class.cpp.o:(.rodata._ZTV5Morse+0xa): undefined reference to `BaseDevice::debugThis()'

So this the message goes away if I add the member debugThis() into Morse class but then what was the point of inheriting the Debug Class? I didn't want debugThis() in Morse class or do I?

thanks

guix

Hello :)

Try removing one of those "void" in
Code: [Select]

virtual void void debugThis();

mistergreen


Hello :)

Try removing one of those "void" in
Code: [Select]

virtual void void debugThis();



copy & paste problem on my part... original issue still stands.

Nick Gammon

Your design intentions are a bit unclear.

This compiles, but it doesn't print "debug":

Code: [Select]

class BaseDevice
{
  public:
    virtual void read();
    virtual void debugThis() { }
};

class Debug
{
  public:
    virtual void debugThis() { Serial.println ("debug!"); };
};

class Morse : public BaseDevice, public Debug
{
  public:
    Morse(int pin) { }
    virtual void read() { };
  private:
    int _pin;
};

void setup ()
  {
  Serial.begin (115200);

  BaseDevice *morse;
  morse  = new Morse(13);
  morse->debugThis();

  }  // end of setup

void loop () { }


That's because the BaseDevice class doesn't print anything (which is what morse is an instance of, now that you cast it).

Meanwhile changing setup to:

Code: [Select]

void setup ()
  {
  Serial.begin (115200);

  BaseDevice *morse;
  Morse * foo  = new Morse(13);
  foo->debugThis();

  }  // end of setup


Gives:

Code: [Select]

sketch_mar22a.ino: In function 'void setup()':
sketch_mar22a:29: error: request for member 'debugThis' is ambiguous
sketch_mar22a:11: error: candidates are: virtual void Debug::debugThis()
sketch_mar22a:5: error: virtual void BaseDevice::debugThis()


Perhaps you are overthinking this. I'm not a big expert on multiple inheritance, and I'm not really sure what you are really trying to achieve.
http://www.gammon.com.au/electronics

mistergreen

Yeah, this multiple inheritance is more of a pain than a solution. Basically, I want Morse to inherit debugThis() from Debug. That's all. I will have other classes that will not inherit debugThis but will inherit the Abstract BaseDevice.

mistergreen



That's because the BaseDevice class doesn't print anything (which is what morse is an instance of, now that you cast it).



Oh, I see, If I cast it as Morse *morse, it works. I can't cast it as the base class, BaseDevice.

michinyon

the virtual void debugThis() declaration in BaseDevice,    requires you to have an actual implementation of
the function in the derived class from BaseDevice,  which is Morse.    You don't have one.    That's why it is unhappy.

Go Up