当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - 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.

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. 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.

网友答案:

The arrays you have declared (letters/specials/numbers) are C type arrays. They don't have functions or member variables - they are just arrays of QStrings.

If you want a class which wraps arrays then look at std::vector and std::array

Alternatively you can programatically determine the size of the array by doing: sizeof(letters) / sizeof(QString)

网友答案:

You cannot call .size for array. Instead, you can do one of the following for a given array like arr:

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

or

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

and in C++17, one will be able to use:

std::size(arr)

The latter is unified way of getting the size for arrays and other stl containers.

分享给朋友:
您可能感兴趣的文章:
随机阅读: