int main()’: a.cpp:39:39: error: invalid new-expression of abstract class type ‘Student’ Person* persons[3] = {new Student(),new Teacher(),new Principal()}; ^ a.cpp:9:7: note: because the following virtual functions are pure within ‘Student’: class Student :public Person{ ^~~~~~~ a.cpp:6:18: note: virtual void Person::bellRing() const virtual void bellRing()const=0; ^~~~~~~~ a.cpp:39:53: error: invalid new-expression of abstract class type ‘Teacher’ Person* persons[3] = {new Student(),new Teacher(),new Principal()}; ^ a.cpp:18:7: note: because the following virtual functions are pure within ‘Teacher’: class Teacher:public Person{ ^~~~~~~ a.cpp:6:18: note: virtual void Person::bellRing() const virtual void bellRing()const=0; ^~~~~~~~ a.cpp:39:69: error: invalid new-expression of abstract class type ‘Principal’ Person* persons[3] = {new Student(),new Teacher(),new Principal()}; ^ a.cpp:27:7: note: because the following virtual functions are pure within ‘Principal’: class Principal:public Person{ ^~~~~~~~~ a.cpp:6:18: note: virtual void Person::bellRing() const virtual void bellRing()const=0; ^~~~~~~~
时间: 2023-06-04 14:06:46 浏览: 86
这是一个关于编程语言的问题,具体是在C++语言中如何实例化一个类的子类对象。问题中给出的代码中有三个类,分别是Person、Student、Teacher和Principal。其中Person是一个抽象类,其中有一个纯虚函数bellRing()。而Student、Teacher和Principal都继承自Person类。问题中的错误提示是因为在创建Person类的指针数组时,试图实例化抽象类的实例。正确的做法是,创建Student、Teacher和Principal类的实例,而不是创建Person类的实例。因此,可以将代码改为以下形式:
Student* s = new Student();
Teacher* t = new Teacher();
Principal* p = new Principal();
Person* persons[3] = {s, t, p};
注意,这只是其中一种可能的解决方法。具体实现方式可能因编程语言版本和需求而有所不同。
相关问题
invalid new-expression of abstract class type ‘Face68DetectorN::Face68LandmarkDetectorImpl’是什么意思
这是一个编译错误,意思是抽象类型“Face68DetectorN::Face68LandmarkDetectorImpl”不能作为新表达式使用。这意味着试图通过实例化抽象类或其子类来创建对象,但抽象类不能被实例化。应该通过实例化具体的子类来解决此问题。
invalid new-expression of abstract class type
“invalid new-expression of abstract class type”是一个编程错误信息,通常出现在试图创建一个抽象类的新实例时。抽象类是指至少有一个纯虚函数的类,而纯虚函数是没有实现的虚函数。
由于抽象类中有未实现的纯虚函数,因此无法创建该类的实例。如果试图使用“new”关键字创建抽象类的实例,编译器会报告错误消息“invalid new-expression of abstract class type”。
要解决这个问题,需要修改代码以创建具体类的实例,而不是抽象类的实例。或者,如果必须使用抽象类,则需要在具体类中实现抽象类中的纯虚函数,以便可以创建该类的实例。