Alex S.
Alex S.

Reputation: 11

Vectors of Pointers passed into different functions

I'm trying to practice with the usage of the new operator to create objects. I'm having some trouble with adjusting my code to manage the new pointers to the objects that I created.

Here's my code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

struct StudentRecord {
public:
    StudentRecord(
        string id,
        string firstName,
        string lastName,
        int age,
        string phoneNumber,
        double gpa
    ) {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
        PhoneNumber = phoneNumber;
        Age = age;
        Gpa = gpa;
    }

    void display() {
        cout << "   Student ID: " << Id << endl;
        cout << "   First Name: " << FirstName << endl;
        cout << "    Last Name: " << LastName << endl;
        cout << " Phone Number: " << PhoneNumber << endl;
        cout << "          Age: " << Age << endl;
        cout << "          GPA: " << Gpa << endl;
        cout << endl;
    }

    string Id;
    string FirstName;
    string LastName;
    string PhoneNumber;
    int Age;
    double Gpa;

};

void displayStudents(vector<StudentRecord>& students) {

    for (auto student : students) {
        student.display();
    }
}

int main()
{
    ifstream inputFile;
    inputFile.open("TestFile.csv");
    string line = "";

    vector<StudentRecord> students;
    while (getline(inputFile, line)) {

        stringstream inputString(line);

        //StudentId, Last Name, FirstName, Age, Phone Number, GPA
        string studentId;
        string lastName;
        string firstName;
        int age;
        string phone;
        double gpa;
        string tempString;

        getline(inputString, studentId, ',');
        getline(inputString, lastName, ',');
        getline(inputString, firstName, ',');
        getline(inputString, tempString, ',');
        age = atoi(tempString.c_str());
        getline(inputString, phone, ',');
        getline(inputString, tempString);
        gpa = atof(tempString.c_str());

        students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));
        line = "";
    }

    displayStudents(students);
}

Specifically there's an issue here:

students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));

I know that I need to adjust the displayStudents function to take in a vector of pointers to the object, but I'm not sure how to do this.

Upvotes: 0

Views: 50

Answers (1)

lorro
lorro

Reputation: 10880

students is of type vector<StudentRecord>, therefore, you don't need to call new.

If you wanted to practice new, suggest changing it to vector<StudentRecord*>. Note that it's recommended to use a managed pointer type (e.g. unique_ptr, smart_ptr) instead of 'naked' pointers.

If you'd simply like to make the code work, use:

students.emplace_back(studentId, lastName,firstName, age, phone, gpa));

In either case, you might consider std::move() on locally-generated strings that you only use here, i.e., std::move(studentId), std::move(lastName), ... in the arguments of emplace_back() or push_back().

Upvotes: 2

Related Questions