Search code examples
c++pointersvectorcollectionsiostream

Copy data from file into vector of pointers


I'm trying to create a function below called load() that copies all the records from my graduate.dat file into my vector of Record pointers called primaryCollection. I created a Record class with variables that make up each Record, and in my load() function in createReport.cc I attempted to read each line in the file, create a Record object with each line, and then add it to my vector.

The problem is I keep getting the error:

createReport.cc: In static member function ‘static void createReport::load()’:
createReport.cc:25:71: error: expression list treated as compound expression in initializer [-fpermissive]
     Record* record(year, province, degree); //create Record object with this data
                                          ^
createReport.cc:25:71: error: invalid conversion from ‘int’ to ‘Record*’ [-fpermissive]

I'm not sure why this is and I would appreciate some help trying to read the file add each record to my collection.

My graduate.dat file is formatted like below in the format < year province degree >

2000 AB Bachelor's 
2005 AB Bachelor's 
2005 MB College 

primaryCollection is required to be a vector of record pointers and I'm also not allowed to use std::map in this task.

Record.h

#ifndef RECORD_H
#define RECORD_H

    #include <iostream>
    #include <string>

    class Record{
      public:
            Record(int = 0, string = "", string = "");
            ~Record();
        
        private:
            int year;
            string province;
            string degree;
    };
    #endif

Record.cc

#include <iostream>
#include <string>
using namespace std;
#include "Record.h"

Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}

Record::~Record(){}

createReport.h

#ifndef CREATEREPORT_H
#define CREATEREPORT_H

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdlib>

#include "Record.h"

class createReport{
  public:
    createReport();
    static void load();
  
  protected:
    static vector<Record*> primaryCollection; //STL vector of record pointers
  
};
#endif

createReport.cc

#include <iostream>
using namespace std;
#include <string>

#include "createReport.h"

void createReport::load(){
    int year;
    string province, degree;

    ostream_iterator<Record*> outItr(cout);

    ifstream infile("graduate.dat", ios::in); 

    if (!infile) {
        cout << "Error: could not open file" << endl;
        exit(1);
    }

    while (infile >> year >> province >> degree) { //as long as were not at end of file
        Record* record(year, province, degree); //create Record object with this data
        primaryCollection.push_back(record); 
    }
  
    cout<<endl<<"List of Records:"<<endl;
    copy(primaryCollection.begin(), primaryCollection.end(), outItr); //print records
}

Solution

  • The correct syntax here is:

    Record* record = new Record(year, province, degree);
    

    Everything after that is fine, but keep in mind that vector now "owns" the pointers and you're responsible for releasing those allocations somehow. This is where pointer wrappers come in handy.