简体   繁体   English

数组声明错误:“变量'...'周围的堆栈已损坏”

[英]Array Declaration Error: "Stack around the variable '...' was corrupted"

I'm trying to declare my object which is an array, but I seem to be getting this compile error message:我试图声明我的 object 是一个数组,但我似乎收到了这个编译错误消息:

Stack around the variable 'rainFall' was corrupted.变量“rainFall”周围的堆栈已损坏。

I am able to input what I have in my array, but as soon as I am done with input, the command prompt closes and then gives me the error.我可以输入数组中的内容,但是一旦完成输入,命令提示符就会关闭,然后给出错误。

const int num = 12;

class Stats
{
private:
    double stats[num];
public:
    Stats();
    void setValue(double stats[num], int);
};

//separate file

void Stats::setValue(double stats[num], int i)
{
    for (i = 0; i < num; i++)
    {
        cout << "What was the rain fall for month # " << i + 1 << " ?" 
        << endl;
        cin >> stats[i];
    }
    if (stats[i] < 0)
    {
        stats[i] = 0;
    }
}

//separate file

int main()
{
    double rainFall[num];

    Stats rainStats[num];

    rainStats[num].setValue(rainFall, num);

    return 0;
}
void Stats::setValue(double stats[num], int num)
{
    for (int i = 0; i < num; i++)
    {
        cout << "What was the rain fall for month # " << i + 1 << " ?" << endl;
        cin >> stats[i];

        if (stats[i] < 0)
        {
            stats[i] = 0;
        }   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM