Multiple inheritance problem

I have 2 base classes

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

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

Child class.

class Morse : public BaseDevice, public Debug
{
  public:
    Morse(int pin) { _pin = pin; }
    void read() { Serial.print("read"); }
  private:
    int _pin;
};
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

Hello :)

Try removing one of those "void" in

virtual void void debugThis();

guix: Hello :)

Try removing one of those "void" in

virtual void void debugThis();

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

Your design intentions are a bit unclear.

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

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:

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

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

  }  // end of setup

Gives:

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.

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.

[quote author=Nick Gammon link=topic=155683.msg1167086#msg1167086 date=1363915711]

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

[/quote]

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

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.