HackerRank's Virtual function Problem some vtable error

Viewed 16

I was solving this question on hacker rank and it used the concept of virtual functions, vptr, and vtable, I just can't figure out what the compiler wants to say and how to I correct it. It seems to be some error with vtable although I have no prior experience with how virtual functions work it was my first program that failed eventually

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 class Person
 {
     public:
     int mem_Name;
     int age;
     virtual void getdata();
     virtual void putdata();
 };
 class Professor : public Person
 {
     public:
     int publ;
     int cur_id=0;
     void getdata()
     {
         cin>>mem_Name;
         cin>>age;
         cin>>publ;
     }
 void putdata()
 {
     cout<<mem_Name<<" "<<age<<" "<<publ<<" "<<cur_id<<endl;
 }
 Professor()
 {
     cur_id++;
 }
 };
  class Student : public Person
 {
     public:
     int marks[6];
     int cur_id=0;
     int sum;
     void getdata()
     {
     cin>>mem_Name;
     cin>>age;
     for(int i=0;i<6;i++)
     {cin>>marks[i];
     sum+=marks[i];}
 }
 void putdata()
 {
     cout<<mem_Name<<" "<<age<<" "<<sum<<" "<<cur_id<<endl;
 }
 Student()
 {
     cur_id++;
 }
 };
int main(){

int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];

for(int i = 0;i < n;i++){

    cin>>val;
    if(val == 1){
        // If val is 1 current object is of type Professor
        per[i] = new Professor;

    }
    else per[i] = new Student; // Else the current object is of type Student

    per[i]->getdata(); // Get the data from the user.

}

for(int i=0;i<n;i++)
    per[i]->putdata(); // Print the required output for each object.

    return 0;

}

The error goes something like thisenter image description here

0 Answers
Related