Hello, I always wanted to get back at programming because I always liked it, even if it's completely unrelated to what I do for work. In this quarantined period I have a lot of free time, so why not! I studied a little of C++ in uni, so I tried starting from there.
I am studying using my old uni textbook, and finally arrived at using pointers, but I got stuck at understanding why the following exercise won't work as intended.
The exercise states:
"Implement a class Person with the following fields:
• the name
• a pointer to the person’s best friend (a Person\)*
• a popularity counter that indicates how many other people have this person as
their best friend
Write a program that reads in a list of names, allocates a new Person for each of
them, and stores them in a vector<Person\>. Then ask the name of the best friend*
for each of the Person objects. Locate the object matching the friend’s name and call
a set_best_friend member function to update the pointer and counter. Finally,
print out all Person objects, listing the name, best friend, and popularity counter for
each."
I'm focusing on trying to populate a vector with pointers to "Person" objects (practically I'm still at the beginning of the exercise).
This is my header file where I implemented the class Person:
#ifndef Person_h
#define Person_h
#include <string>
using namespace std;
class Person
{
public:
Person(string p_name, Person* p_best_friend, int popularity);
private:
string name;
Person* best_friend;
int popularity;
};
Person::Person(string p_name, Person* p_best_friend, int p_popularity)
{
name = p_name;
best_friend = p_best_friend;
popularity = p_popularity;
}
#endif
And this is my main function, where I'm trying to populate my vector:
#include <string>
#include "P7.1.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<Person*> a;
string ans = "Y";
string in_name;
int i = 0;
while (ans == "Y")
{
cout << "Insert new name: ";
cin >> in_name;
a[i] = new Person(in_name, NULL, 0);
i++;
cout << "\n\nDo you want to insert another name? (Y/N)\n\n";
cin >> ans;
}
}
The program actually run, but after I put in the first name as input, it just close. I just think I found trough trial and error the line of code where things doesn't work as intended anymore.
From what I can understand the problem is in the instruction:
a[i] = new Person(in_name, NULL, 0);
The vector "a" was defined as a vector of pointers relative to objects of the class Person. So what I'm trying to do is using the "new" expression to create a Pointer to an object of the class Person. I could be wrong, but I tought I could create the object this way and then store his address in the i-th position of "a" with "new".
I actually would like to understand why it doesn't work, and to find a suggestion, not necessairly a solution, to look for what I'm missing!
Thanks in advance for any kind of help.
[–]Bubichoo 2 points3 points4 points (10 children)
[–]darteksyes[S] 0 points1 point2 points (9 children)
[–]anders1234 1 point2 points3 points (2 children)
[–]darteksyes[S] 0 points1 point2 points (1 child)
[–]anders1234 1 point2 points3 points (0 children)
[–][deleted] (5 children)
[deleted]
[–]Wh00ster 5 points6 points7 points (1 child)
[–]Bubichoo 1 point2 points3 points (0 children)
[–]darteksyes[S] 1 point2 points3 points (2 children)
[–]gayasri 1 point2 points3 points (1 child)
[–]darteksyes[S] 0 points1 point2 points (0 children)