无法使用{0}

时间:2017-03-09 17:34:32

标签: visual-c++ visual-studio-2015 runtime-error

当我在Veccreator函数中初始化float数组时,我的编译器给出了运行时错误。我在这里只发布了我的代码的样本。

#include<iostream>

using namespace std;
#define SIZE 1000
class Vector
{
private:
    float vecarray[SIZE];
public:
    void VecCreator(int dimension)
    {
        vecarray[SIZE]= { 0 };
        cout << "Enter " << dimension << " digits" << endl;
        for (int i = 0; i < dimension; i++)
        {
            cin >> vecarray[i];
        }
    }


};
int main(void) {
    Vector obh;
    obh.VecCreator(2);
}

但它适用于此:`

#include<iostream>

using namespace std;
#define SIZE 1000
class Vector
{
private:
    float vecarray[SIZE]= {0};
public:
    void VecCreator(int dimension)
    {
        cout << "Enter " << dimension << " digits" << endl;
        for (int i = 0; i < dimension; i++)
        {
            cin >> vecarray[i];
        }
    }


};
int main(void) {
    Vector obh;
    obh.VecCreator(2);
}

请告诉我为什么第一个代码会出错。

1 个答案:

答案 0 :(得分:0)

请看第二个答案: https://social.msdn.microsoft.com/Forums/vstudio/en-US/14e7318e-6fff-4d68-a823-9cbe7b7bc20a/debugging-runtime-check-failure-2-stack-around-the-variable-loggerthread-was-corrupted?forum=vcgeneral

为什么不在下面这样做呢?我的意思是如果你想把值放在那里,为什么最初把0放在那里?

private:
    float vecarray[SIZE];
public:
    void VecCreator(int dimension)
    {
        cout << "Enter " << dimension << " digits" << endl;
        for (int i = 0; i < dimension; i++)
        {
            cin >> vecarray[i];
        }
    }