Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在C++子类中使用虚函数_C++_C++14_Virtual Inheritance - Fatal编程技术网

无法在C++子类中使用虚函数

无法在C++子类中使用虚函数,c++,c++14,virtual-inheritance,C++,C++14,Virtual Inheritance,我正在做一个虚拟函数程序,里面有三个班,一个是人,一个是教授,一个是学生。该人由教授和学生公开继承。这里我想在教授和学生中输入一些参数。为了输入和输出它们,我在每个类中创建了getdata和putdata。在person类中,这些函数是虚拟定义的。但是在代码中我有一些错误,例如- Solution.cpp: In function ‘int main()’: Solution.cpp:84:26: error: invalid new-expression of abstract class t

我正在做一个虚拟函数程序,里面有三个班,一个是人,一个是教授,一个是学生。该人由教授和学生公开继承。这里我想在教授和学生中输入一些参数。为了输入和输出它们,我在每个类中创建了getdata和putdata。在person类中,这些函数是虚拟定义的。但是在代码中我有一些错误,例如-

Solution.cpp: In function ‘int main()’:
Solution.cpp:84:26: error: invalid new-expression of abstract class type ‘Professor’
             per[i] = new Professor;
                          ^~~~~~~~~
Solution.cpp:20:7: note:   because the following virtual functions are pure within ‘Professor’:
 class Professor : public Person
       ^~~~~~~~~
Solution.cpp:16:18: note:   ‘virtual void Person::putdata()’
     virtual void putdata() = 0;
                  ^~~~~~~
Solution.cpp:87:27: error: invalid new-expression of abstract class type ‘Student’
         else per[i] = new Student; // Else the current object is of type Student
                           ^~~~~~~
Solution.cpp:42:7: note:   because the following virtual functions are pure within ‘Student’:
 class Student: public Person
       ^~~~~~~
Solution.cpp:16:18: note:   ‘virtual void Person::putdata()’
     virtual void putdata() = 0;
                  ^~~~~~~  
此功能:

void pushdata()
    {
        cout<<name<<" "<<age<<" "<<publications<<"\n";
    }
请注意,如果对要重写的函数进行重写,如果出错,编译器将给出一个错误。

putdata和pushdata不是一回事。很高兴了解override关键字。
void pushdata()
    {
        cout<<name<<" "<<age<<" "<<publications<<"\n";
    }
void putdata() override
    {
        cout<<name<<" "<<age<<" "<<publications<<"\n";
    }