简体   繁体   中英

error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::vector<char,std::allocator<char>>'

I am quite new to C++ and I have gotten the error:

error C2679: binary '\<\<': no operator found which takes a right-hand operand of type 
'std::vector\<char,std::allocator\<char>>' (or there is no acceptable conversion)

Here's the code:

#include <iostream>
#include <random>
#include <vector>
using namespace std;

int main() 
{
    std::random_device rd;
    std::mt19937 gen(rd());

    int i = 0;
    while (i == 0) 
    {
        int password_length;
        cout << "How many characters do you want in the password: ";
        cin >> password_length;

        if (password_length == 0)
        {
            cout << "Bye...";
            break;
        }
        else if (password_length < 5 || password_length > 15)
        {
            cout << "Password must be between 5 and 15 characters." << endl << endl;
            return main();
        }

        int c = 0;
        int y = 0;
        int random_character_case;
        std::vector<char> values(password_length);
        for (int x = password_length; x > 0; x--) 
        {
            if (y == password_length - 2) 
            {
                random_character_case = 1;
            }
            if (c == 2) 
            {
                std::uniform_int_distribution<int> character_ascii_distr(2, 3);
                random_character_case = character_ascii_distr(gen);
            }
            else 
            {
                std::uniform_int_distribution<int> character_ascii_distr(1, 3);
                random_character_case = character_ascii_distr(gen);
            }

            switch (random_character_case)
            {
            case 1: {
                std::uniform_int_distribution<> numbers_ascii_distr(48, 57);
                char asciiValue = numbers_ascii_distr(gen);
                values.push_back(asciiValue);
                c++;
                break;
            }
            case 2: {
                std::uniform_int_distribution<> capital_ascii_distr(65, 90);
                char asciiValue = capital_ascii_distr(gen);
                values.push_back(asciiValue);
                y++;
                break;
            }
            case 3: {
                std::uniform_int_distribution<> lowercase_ascii_distr(97, 122);
                char asciiValue = lowercase_ascii_distr(gen);
                values.push_back(asciiValue);
                y++;
                break;
            }
            }
        }

        cout << values;
        values.clear();
        cout << endl << endl;
    }
}

Trying to write code that generates a set of random characters at a length of user input, with it being a mix of random lowercase and uppercase characters, and two random digits at random places in the set.

I want to emphasize I am quite new; Therefore I am also look forward to see more efficient way to go about what I want to do, if there is any.

At this line

cout << values;

you are trying to call the std::basic_ostream::operator<< for vector of char s (ie std::vector<char> values ). This is not defined, therefore the compiler does not know about it. Hence, the error.

You need to iterate through the elements of the vector of char s (ie values ). For example, using range based for loop:

for (const char ele : values)
    std::cout << ele << " ";

That being said, you could have used simply std::string instead of std::vector<char> for which you have already operator<< available from the standard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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