C++ 错误 : "Array must be initialized with a brace enclosed initializer"

标签 c++ arrays compiler-errors

我收到以下 C++ 错误:

array must be initialized with a brace enclosed initializer 

从这行C++

int cipher[Array_size][Array_size] = 0;

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

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;
}

最佳答案

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

int array[10] = { 0 };

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

对于多维数组,需要嵌套花括号,如下所示:

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

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

关于C++ 错误 : "Array must be initialized with a brace enclosed initializer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4329324/

相关文章:

c++ - 如何从 gcc 的预编译头文件中获益最多?

c++ - 将多维数组加载到 C++ 中的数据文件中

java - 标记上的语法错误、结构错误 - 二分搜索方法中的错误

c++ - 在 C++ 中静态构造函数的实现不起作用

C++ 流二次插入运算符

java - 在给定根文件夹时,在java字符串数组中创建包含路径

java - 通过字节 block 比较两个文件java

android - 程序类型已经存在 : com. google.common.util.concurrent.internal.InternalFutureFailureAccess

python-3.x - 名称错误: name 'get_ipython' is not defined

c++ - 我正在阅读的代码中有奇怪的括号