c++ - 如何解决错误 : ISO C++ forbids variable-size array

标签 c++ arrays variables

我不断收到一条错误消息,提示 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 << "Levels\tScores\tStars" << 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");
}

最佳答案

除非您(或您的老师,如果您将此作为家庭作业)打算做得不好,否则您应该简单地转换 MAXconst .

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

只要你愿意:

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

使用这些,我们最终得到如下代码:

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()) .

关于c++ - 如何解决错误 : ISO C++ forbids variable-size array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899585/

相关文章:

php - 如何在 PHP+HTML+SQL 中使用带空格的 GET 传递表单中的变量

c++ - 在 STL 中使用字符串中的删除关键字?

C++ 作用域和 Google V8 脚本上下文

java - 在 Java 中查找 char 数组的大小

java - 存储在数组中的顺序用户输入

powershell - 喜欢在变量中获取 powershell 脚本的输出以在批处理文件中使用

python - 需要 python multiprocessing.lock() 的帮助

C# 通用行为与 C++ 模板

c++ - 使用 OpenCV 库查找以英寸为单位的图像尺寸

python - 如何创建一个具有相同值的 N 个数字的 numpy 数组?