My toString() not working


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include "Conference.h"
#include "Event.h"
#include "Person.h"
#include "Lesson.h"
#include <string>
#include <vector>
using namespace std;

int returnSelection()
{
   cout << "Enter event type \n(1 for Lesson, 2 for Conference, -1 to exit):";
    int selection;
    cin >> selection;
    return selection;
}

Person * createPerson(string title)
{
    cout << "Enter the first name: ";
    string fn; cin >> fn;
    cout << "Enter the last name: ";
    string ln; cin >> ln;
    cout << "Enter the phone number: ";
    string pn; cin >> pn;
    Person person1 (fn, ln, pn);
    Person* personptr {&person1};
    return personptr;

}
Event * createLesson()
{
    cout << "LESSON CREATION:\n";
    cout << "Enter the starting data: ";
    string sd; cin >> sd;
    cout << "Enter the ending data: ";
    string ed; cin >> ed;
    cout << "Enter the location: ";
    string loc; cin >> loc;
    cout << "Enter the course name: ";
    string cn; cin >> cn;

    Lesson lesson1(sd, ed, loc, cn, *createPerson("instructor"));
    Lesson* lessonptr{&lesson1};
    return lessonptr;
  //  cout << lesson.toString();
}
Event * createConference()
{
   //Your code should come here
}
Event * createEvent(int selection)
{
    if(selection == 1)
        return createLesson();
    else if (selection == 2)
        return createConference();
    else return 0;
}
int main() {

    cout << createEvent(returnSelection())->toString();


    //vector <Event*> events;
    //events.push_back(createEvent(returnSelection()));

    //for (int i=0; i<events.size(); i++)
    //cout << events.at(i)->toString();


    return 0;
}


My tostring() in lesson.cpp file:

string Lesson::toString(){
ostringstream output;
output << Event::toString() << "\nCourse Name: " << getCourseName() << "\nInstructor:\n" << instructor.toString() << endl;
return output.str();

The code runs but the unit the toString part, this is the only output:
Enter event type
(1 for Lesson, 2 for Conference, -1 to exit):1
LESSON CREATION:
Enter the starting data: rcfvg
Enter the ending data: fcvghb
Enter the location: cvghbjn
Enter the course name: cfvghbjn
Enter the first name: cfvghb
Enter the last name: fgvhbjn
Enter the phone number: dxcfvg

Process returned -1073741819 (0xC0000005) execution time : 13.335 s
Press any key to continue.
26
27
28
    Person person1 (fn, ln, pn);
    Person* personptr {&person1};
    return personptr;

43
44
45
    Lesson lesson1(sd, ed, loc, cn, *createPerson("instructor"));
    Lesson* lessonptr{&lesson1};
    return lessonptr;

You are returning a local object by pointer. The object the pointer points to no longer exists after the function ends.
Accessing this not-an-object is then illegal.

Looks like you're attempting to do some sort of polymorphism?
Can you return Lesson and Person by value instead? (i.e. return a Person, not a Person*)
If not, you probably need to use dynamic memory.
Last edited on
Person *personptr = new Person(fn, ln, pn);
return personptr;


Lesson *lessonptr = new Lesson(sd, ed, loc, cn, *createPerson("instructor"));
return lessonptr;


I made those changes and it worked. Thanks so much for your help.
Topic archived. No new replies allowed.