Error: array of variable length of Non-POD element of type 'string'

Before starting, I must first say that I have already considered possible solutions to this error. Unfortunately, they all involve the use of arrays, which is a requirement for my project. Also, I am currently taking Intro on CS, so my experience is close to not having it.

The purpose of the array is to collect names from a file. Therefore, to initialize the array, I count the number of names and use it as the size. The problem is the error indicated in the header, but I see no way to get around it while still using the 1D array.

main.cpp

    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    #include <iostream>
    #include "HomeworkGradeAnalysis.h"

    using namespace std;

    int main()
    {
        ifstream infile;
        ofstream outfile;
        infile.open("./InputFile_1.txt");
        outfile.open("./OutputfileTest.txt");

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

        string str;
        int numLines = 0;

        while (infile)
        {
            getline(infile, str);
            numLines = numLines + 1;
        }
        infile.close();
        int numStudents = numLines - 1;
        int studentGrades[numStudents][maxgrades];
        string studentID[numStudents];

        infile.open("./InputFile_1.txt");

        BuildArray(infile, studentGrades, numStudents, studentID);

        infile.close();
        outfile.close();
        return 0;
    }

HomeworkGradeAnalysis.cpp

    using namespace std;

    void BuildArray(ifstream& infile, int studentGrades[][maxgrades], 
            int& numStudents, string studentID[])
    {
        string lastName, firstName;
        for (int i = 0; i < numStudents; i++)
        {
            infile >> lastName >> firstName;
            studentID[i] = lastName + " " + firstName;
            for (int j = 0; j < maxgrades; j++)
                infile >> studentGrades[i][j];
            cout << studentID[i] << endl;
        }
        return;
    }

HomeworkGradeAnalysis.h

    #ifndef HOMEWORKGRADEANALYSIS_H
    #define HOMEWORKGRADEANALYSIS_H

    const int maxgrades = 10;

    #include <fstream>

    using namespace std;

    void BuildArray(ifstream&, int studentGrades[][maxgrades], int&, string studentID[]);
    void AnalyzeGrade();
    void WriteOutput();

    #endif

Text files have a simple format:

    Boole, George   98    105    0    0    0    100    94    95    97    100

Each row is similar to this, with a different number of students.

, ?

+4
2

, . , .

string studentID[numStudents]; //wrong

string *studentID = new string[numStudents]; //right

EDIT: , .

delete [] studentID
+25

. .

. Clang, g++ - 4.9 , . .

+1
source

All Articles