以下是代码:
class Identity{ public: string m_Name;//姓名 string m_Pwd;//密码 virtual void showSubMenu();//不同身份的子菜单 }; class Student{ public: virtual void showSubMenu(); }; void myLogIn(string filename, int type) { //创建父类指针——注意不是创建父类对象,而是创建父类的指针或引用 Identity m_identity; }
编译出错:
H:Project3 6_reservation_systemidentity.h:5: error: undefined reference to `vtable for Identity'
分析解决:
父类的虚函数表没有指向子类的本应该有的虚函数表,原因是在这里创建了一个父类的对象,而父类是抽象类,无法初始化对象,解决办法是创建一个父类的指针或者引用,即Identity *m_identity。
且父类中的虚函数要么【空实现】,要么让其【=0】。