virtual methods in multiple inheritance

R

ronak2121

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
 
V

Victor Bazarov

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
 
G

Gianni Mariani

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();
}
 
G

Gianni Mariani

Victor said:
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
 
F

Fei Liu

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?
 
R

ronak2121

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top