Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multiple Inheritance Problem [modified]

Multiple Inheritance Problem [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpdesignoopquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patrick G
    wrote on last edited by
    #1

    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 Patrick

    modified on Friday, July 4, 2008 2:27 PM

    D M B 3 Replies Last reply
    0
    • P Patrick G

      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 Patrick

      modified on Friday, July 4, 2008 2:27 PM

      D Offline
      D Offline
      Dr Emmett Brown
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • D Dr Emmett Brown

        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

        P Offline
        P Offline
        Patrick G
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Patrick G

          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 Patrick

          modified on Friday, July 4, 2008 2:27 PM

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          • P Patrick G

            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 Patrick

            modified on Friday, July 4, 2008 2:27 PM

            B Offline
            B Offline
            BadKarma
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • B BadKarma

              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.

              P Offline
              P Offline
              Patrick G
              wrote on last edited by
              #6

              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 |
              ----------- \/-------------

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups