C ++的错误:"数组必须用一个括号括起来的初始化&QUOT初始化; [英] C++ error: "Array must be initialized with a brace enclosed initializer"

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

问题描述

我收到以下C ++错误:

 数组必须用一个括号括起来的初始化初始化

这是此行的C ++的

  INT密码[ARRAY_SIZE] [ARRAY_SIZE];

这里有什么问题吗?是什么错误呢?以下是完整code:

 字符串解密(字符串todecrypt)
{
    INT密[ARRAY_SIZE] [ARRAY_SIZE] = 0;
    字符串密码code = todecrypt.substr(0,3);
    todecrypt.erase(0,3);
    德codecipher(密码code,密码);
    串解密=;
    而(todecrypt.length()大于0)
    {
        串unit_decrypt = todecrypt.substr(0,ARRAY_SIZE);
        todecrypt.erase(0,ARRAY_SIZE);
        诠释tomultiply [ARRAY_SIZE] = 0;
        的for(int i = 0; I< ARRAY_SIZE;我++)
        {
            tomultiply [I] = INT(unit_encrypt.substr(0,1));
            unit_encrypt.erase(0,1);
        }
        的for(int i = 0; I< ARRAY_SIZE;我++)
        {
            INT resultchar = 0;
            对于(INT J = 0; J< ARRAY_SIZE; J ++)
            {
                resultchar + = tomultiply [J] *密码[I] [J]。
            }
            解密+ = CHAR((resultchar%229)-26);
        }
    }
    返回解密;
}


解决方案

静态初始化数组的语法使用大括号,就像这样:

  int数组[10] = {0};

这将零初始化数组。

有关多维数组,你需要嵌套的花括号,就像这样:

  INT密码[ARRAY_SIZE] [ARRAY_SIZE] = {{0}};

注意 ARRAY_SIZE 必须是一个编译时常为这个工作。如果 ARRAY_SIZE 不是在编译时知道的,您必须使用动态初始化。 (preferably,一个的std ::矢量)。

I am getting the following C++ error:

array must be initialized with a brace enclosed initializer 

From this line of C++

int cipher[Array_size][Array_size];

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

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 ++的错误:&QUOT;数组必须用一个括号括起来的初始化&QUOT初始化;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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