Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

arrays

Before beginning, I must first say that I have already looked into possible solutions for this error. Unfortunately, they all have to do with not using arrays, which is a requirement for my project. Also, I'm currently taking Intro to CS so my experience is next to none.

The purpose of the array is to gather names from a file. So in order to initialize array, I count the number of names and use it as the size. The problem is the error stated in the title, but I don't see a way around it while still using a 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

The text files are of simple format:

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

Each line is like this, with varying number of students.

What is another method to which I can still stream the names of the students while still using an array?

like image 789
hanipman Avatar asked Dec 09 '13 01:12

hanipman


People also ask

Why is variable length array bad?

Declaring variable-length arrays can lead to a stack overflow and potential vulnerabilities in the program. Consider the example: void foo(size_t n) { int arr[n]; // .... } Transmission of large number 'n' can lead to a stack overflow as the array will become too large and take up more memory than it really is.

Why are variable length arrays bad in C?

The biggest problem is that one can not even check for failure as they could with the slightly more verbose malloc'd memory. Assumptions in the size of an array could be broken two years after writing perfectly legal C using VLAs, leading to possibly very difficult to find issues in the code.


2 Answers

An Array must be declared with a constant value, you can't use a variable. if you want to declared it using variables you must use a dynamically allocated array.

string studentID[numStudents]; //wrong

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

EDIT: Be sure to free the array once your finished with it

delete [] studentID
like image 74
mari0-k Avatar answered Oct 05 '22 23:10

mari0-k


Variable-lengthed arrays is not standard feature of the language. You have to allocate on the heap or create a vector or use a constant instead.

Besides. I got this error message from Clang, whereas g++-4.9 does't give it to me and compilation is ok. So it's compiler-dependent.

like image 24
Harold Ran Avatar answered Oct 06 '22 00:10

Harold Ran