473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

virtual methods in multiple inheritance

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
5 2080
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
3
by: Milan Cermak | last post by:
Hi all, I have sort of philosophical question. Contructor and destructor behavior points me to this. Is table of virtual methods held in object? Do I get it right that every created object has...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.