By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

virtual methods in multiple inheritance

P: n/a
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 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ro*******@gmail.com 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
Apr 17 '06 #2

P: n/a
ro*******@gmail.com 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();
}
Apr 17 '06 #3

P: n/a
Victor Bazarov wrote:
ro*******@gmail.com 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
Apr 17 '06 #4

P: n/a

ro*******@gmail.com 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?

Apr 20 '06 #5

P: n/a
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 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.