C++ 错误:非 POD 元素类型“字符串”的可变长度数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20461424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:09:54  来源:igfitidea点击:

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

c++arrays

提问by hanipman

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.

在开始之前,我必须首先说我已经研究了这个错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我的项目的要求。另外,我目前正在学习 CS 入门,所以我的经验几乎没有。

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

作业成绩分析.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

作业成绩分析.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?

什么是我仍然可以在仍然使用数组的同时流式传输学生姓名的另一种方法?

回答by mari0-k

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

回答by Harold Ran

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.

除了。我从 Clang 收到了这个错误消息,而 g++-4.9 没有给我它,编译没问题。所以它依赖于编译器。