Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
class Base
{
private:
    int m_age;
protected:
    int m_rank;
public:

    Base() { cout <<"Base created"<<endl;}

    //virtual // Ensures to invoke actual object destructor
    ~Base() { cout<<"Base destroyed"<<endl;}

    virtual void DisplayAction();
};


class Derived1 : public Base
{
public:
    Derived1()
    {
        cout << "Derived1 created" << endl;
    }

    ~Derived1()
    {
        cout << "Derived1 destroyed" << endl;
    }
    //void setage(int m){ m_age=m;}
    void setrank(int m){ m_rank=m;}
    void DisplayAction(){cout <<" rank= "<<m_rank;}

};
Posted
Comments
nv3 6-Oct-15 4:01am    
It would help if you told which error you are getting. How could we guess.
the_beginner 6-Oct-15 4:20am    
||=== Build: Debug in Learning-1 (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `ZN4BaseC2Ev':|
main.cpp|13|undefined reference to `vtable for Base'|
obj\Debug\main.o||In function `ZN4BaseD2Ev':|
main.cpp|16|undefined reference to `vtable for Base'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

here line 13 is Base constructor and line 16 is Base Destructor

1 solution

Because you didn't define it. Either define it, e.g.
C++
virtual void DisplayAction(){cout << "(Base) rank= " << m_rank;}
}; // end of Base


or make it pure virtual, e.g.
C++
virtual void DisplayAction() = 0;
}; // end of Base
 
Share this answer
 
Comments
the_beginner 6-Oct-15 4:22am    
Thanks
CPallini 6-Oct-15 4:34am    
You are welcome.
Maciej Los 7-Oct-15 2:08am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900