Multiple Inheritance Problem [modified]
-
Hello! I have a bit of an odd problem. I'm writing some code that pulls functionality from two different APIs. I also, for what I'm trying to do, I need to inherit from two abstract classes, however I have a bit of a problem... both abstract classes have a method by the same name (but with a different return type), and of coarse, one of them is virtual and I'm trying to overwrite it. The other class does not have this method as virtual, and I am not at all interested in overwriting it. Here's what I'm describing in terms of code...
class BaseA {
int run(int argc, char** argv) {
// insert some code
}
};class BaseB {
virtual void run() = 0;
};class MyClass: public BaseA, BaseB {
void ReactorServer::run() { // Overwrite the virtual method in BaseB (which has nothing to do with BaseA)
// My code here
}
}The problem is that the comiler complains with the following:
Error 1 error C2555: 'MyClass::run': overriding virtual function return type differs and is not covariant from 'BaseA::run' c:\Documents and Settings\patrick\MyClass.h 126
which indicates to me that the compiler is trying to overwrite the method in BaseA and not BaseB. I've tried putting "virtual" in front of my declaration without success. This also bothers me from the perspective that this is a bit of a "smelly" design issue as well, but at the moment I don't think I have a choice because I need these Base Classes! Any suggestions? Thank you Patrickmodified on Friday, July 4, 2008 2:27 PM
-
Hello! I have a bit of an odd problem. I'm writing some code that pulls functionality from two different APIs. I also, for what I'm trying to do, I need to inherit from two abstract classes, however I have a bit of a problem... both abstract classes have a method by the same name (but with a different return type), and of coarse, one of them is virtual and I'm trying to overwrite it. The other class does not have this method as virtual, and I am not at all interested in overwriting it. Here's what I'm describing in terms of code...
class BaseA {
int run(int argc, char** argv) {
// insert some code
}
};class BaseB {
virtual void run() = 0;
};class MyClass: public BaseA, BaseB {
void ReactorServer::run() { // Overwrite the virtual method in BaseB (which has nothing to do with BaseA)
// My code here
}
}The problem is that the comiler complains with the following:
Error 1 error C2555: 'MyClass::run': overriding virtual function return type differs and is not covariant from 'BaseA::run' c:\Documents and Settings\patrick\MyClass.h 126
which indicates to me that the compiler is trying to overwrite the method in BaseA and not BaseB. I've tried putting "virtual" in front of my declaration without success. This also bothers me from the perspective that this is a bit of a "smelly" design issue as well, but at the moment I don't think I have a choice because I need these Base Classes! Any suggestions? Thank you Patrickmodified on Friday, July 4, 2008 2:27 PM
You shouldn't have any problems in doing what you're trying. The only detail I see is the way you are declaring the run method in the derived class. Why did you include "ReactorServer::" ?
rotter The metaller programmer
-
You shouldn't have any problems in doing what you're trying. The only detail I see is the way you are declaring the run method in the derived class. Why did you include "ReactorServer::" ?
rotter The metaller programmer
Sorry about that typo, that's the "real" name of MyClass (my program is for a reactor server in a distributed system). The should say
MyClass::run()
. Also, I declare the classes in h files and code them in cpp files separately, which is why I have that there in the first place.
-
Hello! I have a bit of an odd problem. I'm writing some code that pulls functionality from two different APIs. I also, for what I'm trying to do, I need to inherit from two abstract classes, however I have a bit of a problem... both abstract classes have a method by the same name (but with a different return type), and of coarse, one of them is virtual and I'm trying to overwrite it. The other class does not have this method as virtual, and I am not at all interested in overwriting it. Here's what I'm describing in terms of code...
class BaseA {
int run(int argc, char** argv) {
// insert some code
}
};class BaseB {
virtual void run() = 0;
};class MyClass: public BaseA, BaseB {
void ReactorServer::run() { // Overwrite the virtual method in BaseB (which has nothing to do with BaseA)
// My code here
}
}The problem is that the comiler complains with the following:
Error 1 error C2555: 'MyClass::run': overriding virtual function return type differs and is not covariant from 'BaseA::run' c:\Documents and Settings\patrick\MyClass.h 126
which indicates to me that the compiler is trying to overwrite the method in BaseA and not BaseB. I've tried putting "virtual" in front of my declaration without success. This also bothers me from the perspective that this is a bit of a "smelly" design issue as well, but at the moment I don't think I have a choice because I need these Base Classes! Any suggestions? Thank you Patrickmodified on Friday, July 4, 2008 2:27 PM
You're leaving something out in your code sample. This works fine:
class BaseA
{
int run(int argc, char** argv)
{
return 0;
}
};class BaseB
{
virtual void run() = 0;
};class MyClass: public BaseA, BaseB
{
void run()
{
// Override the virtual method in BaseB (which has nothing to do with BaseA)
}
};And it's override, not overwrite :)
-
Hello! I have a bit of an odd problem. I'm writing some code that pulls functionality from two different APIs. I also, for what I'm trying to do, I need to inherit from two abstract classes, however I have a bit of a problem... both abstract classes have a method by the same name (but with a different return type), and of coarse, one of them is virtual and I'm trying to overwrite it. The other class does not have this method as virtual, and I am not at all interested in overwriting it. Here's what I'm describing in terms of code...
class BaseA {
int run(int argc, char** argv) {
// insert some code
}
};class BaseB {
virtual void run() = 0;
};class MyClass: public BaseA, BaseB {
void ReactorServer::run() { // Overwrite the virtual method in BaseB (which has nothing to do with BaseA)
// My code here
}
}The problem is that the comiler complains with the following:
Error 1 error C2555: 'MyClass::run': overriding virtual function return type differs and is not covariant from 'BaseA::run' c:\Documents and Settings\patrick\MyClass.h 126
which indicates to me that the compiler is trying to overwrite the method in BaseA and not BaseB. I've tried putting "virtual" in front of my declaration without success. This also bothers me from the perspective that this is a bit of a "smelly" design issue as well, but at the moment I don't think I have a choice because I need these Base Classes! Any suggestions? Thank you Patrickmodified on Friday, July 4, 2008 2:27 PM
-
Hi, Like Rotter512 said it should work, I have tried it with cpp and h files and it still works. I am using VS2005.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
I found the reason for the problem -> There was an unresolvable ambiguity (the compiler couldn't tell which base class to override. Here's what Stroustrup has to say about this issue (see page 5). http://www-plan.cs.colorado.edu/diwan/class-papers/mi.pdf[^] Thanks for your responses. I must admit, I'm curious how you all got it to work. To solve the problem, I ended up writing a wrapper class... please excuse my sad attempt at ASCII art, but here's the UML behind how I solved it.
----------- -----------
| Class A | | Class B |
/\\ /\\ -- -- | | | |
| MyClass |_________/\| MyWrapper |
----------- \/-------------