简体   繁体   English

.size()的左侧必须具有class / struct / union

[英]left of .size() must have class/struct/union

I've been trying to make a random password generator and so far everything has been going good until I got this error which I can't seem to figure out: left of size must have class/struct/union I've looked around and people have similar problems but I can't seem to find a solution for mine. 我一直在尝试制作一个随机的密码生成器,到目前为止,一切都进行得很好,直到我收到似乎无法弄清楚的错误为止:左边的大小必须有我一直在查看的class / struct / union人们有类似的问题,但我似乎找不到我的解决方案。 Any help would is appreciated. 任何帮助将不胜感激。

void MainWindow::on_pushButton_2_clicked()
    {
        QString password;

        QString letters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        QString special[] = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '+', '=', '/', '?', '>', '<', '\'', '\"', ';', ':', '`', '~'};
        int numbers[] = {1,2,3,4,5,6,7,8,9,0};

        int length = ui->horizontalSlider_2->value();
        bool douppercase = ui->checkBox_2->isChecked();

        for(int i = 1; i <= length; i++) {
            int selection = roll(1, 3);
            qDebug() << selection;
            QString selectedletter;
            int lettercase;
            int letter;
            switch(selection) {
                case 2:
                    letter = roll(0, special.size());
                    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    password += special[letter];
                break;
                case 3:
                    letter = roll(0, numbers.size());
                    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    password += numbers[letter];
                break;
                default:
                    letter = roll(0, letters.size());
                    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    selectedletter = letters[letter];
                    if(douppercase)
                        lettercase = roll(1, 2);
                        if(lettercase == 1)
                            selectedletter.toLower();
                        password += selectedletter;
                break;
            }
        }

        ui->lineEdit->setText(password);
    }

    int MainWindow::roll(int min, int max) {
        int randNum = rand()%(max-min + 1) + min;
        return randNum;
    }
letter = roll(0, special.size());

special and letters are arrays. specialletters是数组。 Arrays don't have methods. 数组没有方法。 They don't have class members, they are just arrays. 它们没有类成员,它们只是数组。

You are probably looking for: 您可能正在寻找:

sizeof(specials)/sizeof(specials[0])

Ditto for letters . 同上letters

The arrays you have declared (letters/specials/numbers) are C type arrays. 您声明的数组(字母/特殊字符/数字)是C类型的数组。 They don't have functions or member variables - they are just arrays of QStrings. 它们没有函数或成员变量-它们只是QString的数组。

If you want a class which wraps arrays then look at std::vector and std::array 如果您想要一个包装数组的类,请查看std :: vector和std :: array

Alternatively you can programatically determine the size of the array by doing: sizeof(letters) / sizeof(QString) 或者,您可以通过以下方式以编程方式确定数组的大小:sizeof(letters)/ sizeof(QString)

You cannot call .size for array. 您不能为数组调用.size Instead, you can do one of the following for a given array like arr : 相反,您可以对arr这样的给定数组执行以下操作之一:

sizeof(arr) / sizeof(arr[0])

or 要么

std::end(arr) - std::begin(arr)

and in C++17, one will be able to use: 在C ++ 17中,将可以使用:

std::size(arr)

The latter is unified way of getting the size for arrays and other stl containers. 后者是获取数组和其他stl容器大小的统一方法。

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

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