Pages: [1]   Go Down
Author Topic: Multiple inheritance problem  (Read 254 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 2 base classes

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

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


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



Code:
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
« Last Edit: March 21, 2013, 08:16:15 pm by mistergreen » Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

Try removing one of those "void" in
Code:
virtual void void debugThis();
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

Try removing one of those "void" in
Code:
virtual void void debugThis();

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your design intentions are a bit unclear.

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

Code:
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:
void setup ()
  {
  Serial.begin (115200);

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

  }  // end of setup

Gives:

Code:
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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2895
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: