C++ 错误:“必须使用大括号括起来的初始化程序初始化数组"; [英] C++ error: "Array must be initialized with a brace enclosed initializer"

查看:150
本文介绍了C++ 错误:“必须使用大括号括起来的初始化程序初始化数组";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下 C++ 错误:

I am getting the following C++ error:

array must be initialized with a brace enclosed initializer 

来自这行 C++

int cipher[Array_size][Array_size] = 0;

这里有什么问题?错误是什么意思?完整代码如下:

What is the problem here? What does the error mean? Below is the full code:

string decryption(string todecrypt)
{
    int cipher[Array_size][Array_size] = 0;
    string ciphercode = todecrypt.substr(0,3);
    todecrypt.erase(0,3);
    decodecipher(ciphercode,cipher);
    string decrypted = "";
    while(todecrypt.length()>0)
    {
        string unit_decrypt = todecrypt.substr(0,Array_size);
        todecrypt.erase(0,Array_size);
        int tomultiply[Array_size]=0;
        for(int i = 0; i < Array_size; i++)
        {
            tomultiply[i] = int(unit_encrypt.substr(0,1));
            unit_encrypt.erase(0,1);
        }
        for(int i = 0; i < Array_size; i++)
        {
            int resultchar = 0;
            for(int j = 0; j<Array_size; j++)
            {
                resultchar += tomultiply[j]*cipher[i][j]; 
            }
            decrypted += char((resultchar%229)-26);
        }
    }
    return decrypted;
}

推荐答案

静态初始化数组的语法使用大括号,如下所示:

The syntax to statically initialize an array uses curly braces, like this:

int array[10] = { 0 };

这将对数组进行零初始化.

This will zero-initialize the array.

对于多维数组,你需要嵌套的花括号,像这样:

For multi-dimensional arrays, you need nested curly braces, like this:

int cipher[Array_size][Array_size]= { { 0 } };

请注意,Array_size 必须是编译时常量才能使其正常工作.如果 Array_size 在编译时未知,则必须使用动态初始化.(最好是 std::vector).

Note that Array_size must be a compile-time constant for this to work. If Array_size is not known at compile-time, you must use dynamic initialization. (Preferably, an std::vector).

这篇关于C++ 错误:“必须使用大括号括起来的初始化程序初始化数组";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆