Trouble calling overloaded virtual method

Im trying to create a map container with the key being an ID number and the value being a pointer to a class object. Currently Im creating objects and storing their address in the container. I am getting a runtime error when calling the virtual method with this pointer. I believe that the problem is being called because they aren't being called pointer/reference. let me know if you need more.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if(command == 'F'){
            
            inputDataFile>>name>>mNumber>>email>>department>>tenure;
            faculty newFaculty(name,mNumber,email,department,tenure);
            person* facultyAdd = &newFaculty;
            
    cout<<"Note: Adding "<<mNumber<<"..."<<endl<<"Adding ";
            people.insert(pair<string,person*>(mNumber,facultyAdd));
         }
        
//print based on mNumber 
        if(command == 'P'){
            
            inputDataFile>>mNumber;
            
            cout<<"Note: Printing "<<mNumber<<"..."<<endl;
 
if(people.find(mNumber) == people.end()){
cout<<"Error " <<mNumber<<" Not Found!"<<endl;
}
else{
(*people[mNumber]) -> print();
found = people.find(mNumber);
(*found).second ->print();
}
}
if(command == 'L'){
 
cout<<"Listing People"<<endl;
 
start = people.begin();
stop = people.end();
while(start != stop){
 
(*start).second -> print();
start++;
}
The variable created on line 4 is destroyed on line 9 because it is bound to the scope it was created in. Unfortunately you store its address on line 5 and line 8, which means when it gets destroyed on line 9 you have store a dangling pointer.

Then you try to call functions on memory that you no longer own and has probably been changed since then.

In this case you must use dynamic memory.
LB,

Thanks I changed to dynamic allocation and this resolved the problem.
Don't forget to delete whatever you new ;)
Topic archived. No new replies allowed.