简体   繁体   中英

error: invalid conversion from 'const char*' to 'unsigned char' [-fpermissive]

I plan to use C++ to make a random password generator (replacing python for speed), so I typed characters. When I input all characters, an error occurs:

warning: character constant too long for its type
 const unsigned char chars[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:'" + '",<.>/?'};
                                                                                                       ^~~~~~~~~

main.cpp:3:123: error: invalid conversion from 'const char*' to 'unsigned char' [-fpermissive]
 const unsigned char chars[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:'" + '",<.>/?'};
#include <iostream>

const unsigned char chars[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:'" + '",<.>/?'};
//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?
int main(){
    std::cout << chars;
    return 0;
}

Did you try using std::string they are easier to manipulate and they are more similar to python

You are using '",<.>/?' in the second part. You can normally use ' ' with chars. Use instead following with escape char \

#include <iostream>

const std::string chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";

//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?
int main(){
    std::cout << chars;
    return 0;
}

this is the code (been fixed):

#include <iostream>

using namespace std;

//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?
class generate{
    public:
        void generate_random_password(){
            const char* word = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+[{]}\\|;:',<.>/?\"";
            cout << word;
        }
};
int main(){
    generate test;
    test.generate_random_password();
    int test2;
    std::cin >> test2;
    return 0;
}

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