如何解决错误:ISO C++禁止可变大小的数组

How to solve the error: ISO C++ forbids variable-size array

本文关键字:禁止 数组 C++ ISO 何解决 解决 错误      更新时间:2023-10-16

我不断收到一个错误,说 ISO C++禁止可变大小的数组。

我假设有一个显示的输出

Level       Score          Stars
----------------------------------
1              3840           ***

等等....

这是我的程序

#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<fstream>

using namespace std;
int buildArrays(int A [], int B [], int C [])
{
    int i = 0, num;
    ifstream inFile;
    inFile.open("candycrush.txt");
    if (inFile.fail())
    {
        cout << "The candycrush.txt input file did not open" << endl;
        exit(-1);
    }
    while (inFile)
    {
        inFile >> num;
        A[i] = num;
        inFile >> num;
        B[i] = num;
        inFile >> num;
        C[i] = num;
        i++;
    }
    inFile.close();
    return i;
}
void printArrays(string reportTitle, int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
{
    cout << endl;
    cout << reportTitle << endl;
    cout << "LevelstScorestStars" << endl;
    cout << "---------------------" << endl;
    for (int i = 0; i < numberOfLevels; i++)
    {
        cout << levelsArray[i] << "t" << scoresArray[i] << "t";
        for (int j = 0; j < starsArray[i]; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}
void sortArrays(int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
{
    for (int i = 0; i < numberOfLevels; i++)
    {
        for (int j = 0; j < numberOfLevels; j++)
        {
            if (levelsArray[i] < levelsArray[j])
            {
                int temp1 = levelsArray[i];
                int temp2 = scoresArray[i];
                int temp3 = starsArray[i];
                levelsArray[i] = levelsArray[j];
                scoresArray[i] = scoresArray[j];
                starsArray[i] = starsArray[j];
                levelsArray[j] = temp1;
                scoresArray[j] = temp2;
                starsArray[j] = temp3;
            }
        }
    }
}

int main()
{
    int MAX = 400;         (This is where I am getting my valid array size error)
        int levelsArray[MAX];
    int scoresArray[MAX];
    int starsArray[MAX];
    int numberOfLevels = buildArrays(levelsArray, scoresArray, starsArray);
    printArrays("Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels);
    sortArrays(levelsArray, scoresArray, starsArray, numberOfLevels);
    printArrays("Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels);
    system("pause");
}

除非你(或你的老师,如果你把这当作家庭作业)打算做得不好,否则你不应该简单地将MAX转换为const

相反,您应该使用 std::vector 而不是根本不使用数组。

只要你在:

  1. 创建一个struct来保存单个乐谱的三个部分。
  2. 使用std::sort而不是您自己的排序功能。
  3. 重载operator>>operator<< score对象上执行 I/O。
  4. 更喜欢一步初始化而不是默认构造,然后是真正的初始化(例如,创建,然后单独打开流以及创建然后单独填充向量)。
  5. 切勿使用 while (stream) read_data 1。始终测试读取数据的结果,并对该结果做出反应。

使用这些,我们最终得到这样的代码:

struct score { 
    int level;
    int score;
    int stars;
    bool operator<(score const &other) const { 
        return level < other.level;
    }
    friend std::istream &operator>>(std::istream &is, score &s) { 
        return is >> s.level >> s.score >> s.stars;
    }
    friend std::ostream &operator<<(std::ostream &os, score const &s) { 
         return os << s.level << "t" 
                   << s.score << "t"
                   << std::string(s.stars, '*');
    }
};
int main() { 
    std::ifstream in("candycrush.txt");
    std::vector<score> scores{std::istream_iterator<score>(in),
                              std::istream_iterator<score>()};
    std::cout << "Unsorted:n";
    for (auto const &s : scores)
        std::cout << s << "n";
    std::cout << "n";
    std::sort(scores.begin(), scores.end());
    std::cout << "Sorted:n";
    for (auto const &s : scores) 
        std::cout << s << "n";
}

您可能还应该添加一些东西来处理两个水平相等的分数(例如,在这种情况下通过比较分数),但这可能是另一个答案/谩骂的主题。


1. ...或while (stream.good())while (!stream.eof()).

gcc 支持可变大小数组,但其他编译器不支持。 尝试。

int main()
{
    #define MAX 400        /* use macro instead of stack variable */
    int levelsArray[MAX];
    int scoresArray[MAX];
    int starsArray[MAX];
    /* rest of your code */
    return 0;
}

对于大多数编译器,数组大小应该是C++的编译时常量。尝试

#define MAX 400;
...
int levelsArray[MAX];

const int MAX=400;
...
int levelArray[MAX];

您需要将 MAX 变量设置为常量变量。试试这个:

const int MAX=400;
#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<fstream>
#define MAX 400 //<- Try this
using namespace std;

我还建议在处理多个数组时使用类。通过使用类,您无需将多个数组传递给函数并使用长数组列表设置函数的参数。比如这个:

void printArrays(string reportTitle, int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)

请改为遵循向量用法(当您的目的需要可变大小的数组时): https://stackoverflow.com/a/49021923/4361073