problem with virtual functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahmoodn
    New Member
    • May 2009
    • 77

    #1

    problem with virtual functions

    I have defined to class with virtual functions:
    Code:
    class A {
    public:
    	virtual void write( char ) = 0;
    	virtual void reset() = 0;
    };
    
    class B {
    public:
    	virtual void read( char & ) = 0;
    	virtual int num_available() = 0;
    };
    Then I defined another class that derived from them:
    Code:
    class fifo : public A, public B {
       // some functions
    }
    Now in the main I wrote:
    Code:
    fifo *afifo;
    afifo = new fifo;
    But I get this error:
    main.cpp: In function `int main(int, char**)':
    main.cpp:11: error: cannot allocate an object of type `fifo'
    main.cpp:11: error: because the following virtual functions are abstract:
    fifo.h:6: error: virtual void write_if::reset ()
    fifo.h:12: error: virtual int read_if::num_av ailable()


    What should I do?
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    You need to declare (and implement) said functions in your fifo class.

    Comment

    • mahmoodn
      New Member
      • May 2009
      • 77

      #3
      thanks,
      the functions must be defined in the fifo
      Code:
      class fifo : public A, public B {
      public:
         ...
         void reset() {
            ....
         }
         // and others, read, write, num_avaialbe
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Keep in mind that with multiple inheritance, you will have to call your own indirect base class constructors. Nornmally, your constructor onyl calls your immediate base class but with multiple inheritance this feature is disabled. Unless you call all base class constructors (direct and indirect) in the initialization list of your derived class, those constructors will not be called and you will have a garbage object.

        Comment

        Working...