virtual methods in multiple inheritance

Discussion in 'C++' started by ronak2121@gmail.com, Apr 17, 2006.

  1. Guest

    All,

    I am getting a linking error when trying to use multiple inheritance
    with virtual methods.

    Here is some sample code which illustrates my problem.

    class A {

    };

    class B {
    virtual void method()=0;
    }

    class C : public A, public virtual B{

    virtual void method(){
    //do something in here
    }
    }

    class D : public virtual B{

    }

    class E : public C, public D{

    }

    I am getting a linking error saying that the vtable has an undefined
    reference to the method method(). I do not understand why I am getting
    this error and what I can do to fix it.

    I would think that since class C already provides an implementation,
    class E just inherits that method and everything is happy. Why is the
    linker complaining? How can I fix this?

    I am using gcc 4.1 on Fedora Core 5.

    Thanks in Advance
     
    , Apr 17, 2006
    #1
    1. Advertising

  2. wrote:
    > I am getting a linking error when trying to use multiple inheritance
    > with virtual methods.
    >
    > Here is some sample code which illustrates my problem.
    >
    > class A {
    >
    > };
    >
    > class B {
    > virtual void method()=0;
    > }
    >
    > class C : public A, public virtual B{
    >
    > virtual void method(){
    > //do something in here
    > }
    > }
    >
    > class D : public virtual B{
    >
    > }
    >
    > class E : public C, public D{
    >
    > }
    >
    > I am getting a linking error saying that the vtable has an undefined
    > reference to the method method(). I do not understand why I am getting
    > this error and what I can do to fix it.
    >
    > I would think that since class C already provides an implementation,
    > class E just inherits that method and everything is happy. Why is the
    > linker complaining? How can I fix this?


    What about 'D'? It inherits the pure B::method. And 'E' also inherits
    that (as 'D::method'). You need to do something about that.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 17, 2006
    #2
    1. Advertising

  3. wrote:
    ....
    > I would think that since class C already provides an implementation,
    > class E just inherits that method and everything is happy. Why is the
    > linker complaining? How can I fix this?
    >
    > I am using gcc 4.1 on Fedora Core 5.
    >


    The code below compiled and ran find on gcc (GCC) 4.1.0 20060304 (Red
    Hat 4.1.0-3) (Fedoca Core 5). Your problem seems to be elsewhere.

    class A {

    };

    class B {
    virtual void method()=0;
    };

    class C : public A, public virtual B{
    public:
    virtual void method(){
    //do something in here
    }
    };

    class D : public virtual B{

    };

    class E : public C, public D{

    };

    int main()
    {
    E x;
    x.method();
    }
     
    Gianni Mariani, Apr 17, 2006
    #3
  4. Victor Bazarov wrote:
    > wrote:
    >
    >>I am getting a linking error when trying to use multiple inheritance
    >>with virtual methods.
    >>
    >>Here is some sample code which illustrates my problem.
    >>
    >>class A {
    >>
    >>};
    >>
    >>class B {
    >> virtual void method()=0;
    >>}
    >>
    >>class C : public A, public virtual B{
    >>
    >> virtual void method(){
    >> //do something in here
    >> }
    >>}
    >>
    >>class D : public virtual B{
    >>
    >>}
    >>
    >>class E : public C, public D{
    >>
    >>}
    >>
    >>I am getting a linking error saying that the vtable has an undefined
    >>reference to the method method(). I do not understand why I am getting
    >>this error and what I can do to fix it.
    >>
    >>I would think that since class C already provides an implementation,
    >>class E just inherits that method and everything is happy. Why is the
    >>linker complaining? How can I fix this?

    >
    >
    > What about 'D'? It inherits the pure B::method. And 'E' also inherits
    > that (as 'D::method'). You need to do something about that.


    B is inherited virtually which means there is only one "method()"
    function. i.e. D::Method is the same as C::Method. I don't know what
    the standard specifically says about this but it makes sense that you
    can provide a single definition of "method()" in this case.

    The FC5 gcc compiler actually links it (with a few syntax errors fixed)
    without a problem so the OP needs to go and look a bit harder.

    G
     
    Gianni Mariani, Apr 17, 2006
    #4
  5. Fei Liu Guest

    wrote:
    > All,
    >
    > I am getting a linking error when trying to use multiple inheritance
    > with virtual methods.
    >
    > Here is some sample code which illustrates my problem.
    >
    > class A {
    >
    > };
    >
    > class B {
    > virtual void method()=0;
    > }
    >
    > class C : public A, public virtual B{
    >
    > virtual void method(){
    > //do something in here
    > }
    > }
    >
    > class D : public virtual B{
    >
    > }
    >


    Did you instantiate any object of class D in your code?
     
    Fei Liu, Apr 20, 2006
    #5
  6. Guest

    I fixed this problem. The problem was due to some missing libraries.
    Thanks to the people who created gcc's linker, I wasted a few good
    hours determining what the problem is.
     
    , Apr 25, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    825
    Daniel T.
    Feb 4, 2004
  2. cppsks
    Replies:
    0
    Views:
    853
    cppsks
    Oct 27, 2004
  3. Simas Toleikis
    Replies:
    1
    Views:
    1,972
    Victor Bazarov
    Apr 30, 2005
  4. Ashwin
    Replies:
    2
    Views:
    367
    Pierre Barbier de Reuille
    Aug 1, 2006
  5. Replies:
    1
    Views:
    482
    Alf P. Steinbach
    Mar 14, 2009
Loading...

Share This Page