-1

I'm trying create a C++ program to read the text file and tokenize each field of the text. So far I have this but I have an error that says "Variable length array of non-POD element type 'Employee'" Not sure what to do so I attempted to follow what I read and commented it below the error line but that created more errors.

int main () {
     string line1, line2;
     int i = 1, j=0;
     int numberOfRecords = 0;
     ifstream myInputFile ("emp.txt");
     ofstream myOutputFileSort1, myOutputFileSort2;
     myOutputFileSort1.open("emp3sort1.txt");
     myOutputFileSort2.open("emp3sort2.txt");

if(myInputFile.is_open())
{
    while(getline (myInputFile,line1))
    {
        numberOfRecords++;
    }
    myInputFile.close();
}

**Employee myEmployees [numberOfRecords];**//Variable length array of non-POD element type 'Employee'
//string *myEmployees = new string [numberOfRecords];//attempt to fix
ifstream myInputFileAgain ("emp.txt");
...
tadashi
  • 91
  • 1
  • 9

1 Answers1

1

Variable Length Arrays are not a part of C++. They exist as compiler extensions, but apparently your compiler supports them only for POD (plain old data) types.

You should consider using std::vector instead.

std::vector<Employee> myEmployees(numberOfRecords)

or

std::vector<Employee> myEmployees;
myEmployees.reserve(numberOfRecords); // optional, but recommended

The first creates a "dyanmic array" of numberOfRecords elements of Employee type and you can access them as you wanted (myEmployees[n]), the latter just reserves memory for numberOfRecords employees, but has 0 elements.

krzaq
  • 16,240
  • 4
  • 46
  • 61