Advertisement

Interface Problem

Started by August 20, 2001 06:15 PM
3 comments, last by PsyCHo puNk 23 years, 6 months ago
Conceptually I have an interface structure and two classes, one will be used to implement platform independent portions of the interface and the other will implement the platform specific portions. I am having a problem getting this to work, which I will illustrate with a small weird code example: (contents of INacho.h) struct INacho { virtual void SomeFunc(void) = 0; virtual void ScreamingDeathMonkeys(void) = 0; }; (contents of CNacho.h) class CNacho { public: void SomeFunc(void) { cout << "CNacho::SomeFunc(void)" << endl; } }; (contents of CNacho_Win32.h) #include #include "CNacho.h" // Base class. #include "INacho.h" // Interface. class CNacho_Win32 : public CNacho, public INacho { public: // SomeFunc inherited from CNacho. void ScreamingDeathMonkeys(void) { cout << "RUN FOR YOUR LIVES FROM THE SCREAMING DEATH MONKEYS!!!! WAAAAAH" << endl; } }; Now when I create a client application: #include "CNacho_Win32.h" int main(void) { INacho* pNacho = NULL; pNacho = (INacho*) new CNacho_Win32(); if(pNacho)delete pNacho; return 0; } I get the error cannot instantiate abstract class CNacho_Win32 due to the following members: void __thiscall INacho::SomeFunc(void) pure virtual function was not defined. But this is inherited from CNacho! How can I fix this? thanks, Mike
i believe CNacho would have to inherit INacho;
Advertisement
You're implementing a class that inherents from two classes that have the same signature. I believe that is the source of the problem (ambiguous). If you want CNacho_Win32 to use Nacho's someFunct() then have CNacho extend INacho instead of CNacho_Win32. The way you currently have the classes implemented, there is no relationship between INacho and CNacho which is what I think you're trying to accomplish here.

Given what you are trying to accomplish, why don't you have CNacho and CNacho_Win32 both extend INacho instead of having CNacho_Wind32 extend both CNacho and INacho unless you explicitly want CNacho_Win32 to use CNacho's implementation of someFunct(). To achieve this just have CNacho inherit INacho and CNacho_Win32 extend CNacho. Hope this helps.

Brendon

Edited by - bstepa on August 20, 2001 7:46:41 PM
Well actually I use to (conceptually since this is an example) have CNacho : public INacho then CNacho_Win32 : public CNacho which worked fine, however I got the idea I illustrated above from a OOP + uml design book and it got me thinking, if I add a method to the interface (during its initial design) then I only have to add the implementation to one of the other classes (rather then adding an implemented version to one and a pure virtual function to another). Basically I''m trying to make it so if I change an interface theres less files that need to be modified since im a lazy programmer hehe The idea of having the base class is also so I can share platform independent methods of the interface with only having to type them once (eg. accessor methods and methods only using ansi standard functions). I guess ill have to switch back to my old method since I can''t seem to get this working... ih one more line of typing.. hehehe
You''re not going to get this version to work because of ambiguity caused by multiple inheritance. To achieve what you''re thinking about, do the following:

CNacho inherits INacho
CNacho_Win32 inherits CNacho

Since CNacho_Win32 inherits from CNacho, it also inherits the INacho interface. You only need to implement the CNacho::SomeFunc(). Now you''ll have what you posted.

Brendon

This topic is closed to new replies.

Advertisement