简体   繁体   English

错误:隐式转换改变了符号:'int' 到 'unsigned long'

[英]error: implicit conversion changes signedness: 'int' to 'unsigned long'

I am using clang++ "file.cpp" -std=c++14 -Weverything -Wno-c++98-compat to compile my program.我正在使用 clang++ "file.cpp" -std=c++14 -Weverything -Wno-c++98-compat 来编译我的程序。 Now I get this following error code:现在我得到以下错误代码:

warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]警告:隐式转换改变了符号:'int' 到 'unsigned long' [-Wsign-conversion]

The error line is this:错误行是这样的:

v_rand = rand() % (english.size());

The whole code of error:整个错误代码:

void trainer_es (string lang)
{

    ifstream in("dictionary_es.txt");
    if(!in)
    {
        cout<<"ERROR"<<endl;
        }

    string line;
    int right=0;
    int wrong=0;
    vector<string> english, spanish;
    bool is_english = true; 
    while(in.eof()==0)  
        { 
            getline(in,line);
            if(is_english   
            {
                english.push_back(line);
            }
                else 
            {
            spanish.push_back(line);

            }
        is_english = !is_english;   
        }

    in.close();

    unsigned long v_rand;
    srand(unsigned (time(nullptr)));

    if (lang == "english")
    {
        for (unsigned long int i=0; i<deutsch.size()-1; i++)
        {

            v_rand = rand() % (english.size());
            cout<<"Translate the word: "<<english.at(v_rand)<<endl;
            cin>>line;

            if(line == spanish.at(v_rand))
            {
                cout<<"right!"<<endl;
                right++;
            }

            else 
            {
                cout<<"wrong"<<endl;
                cout<<"the correct word is: "<<spanish.at(v_rand)<<endl;
                wrong++;
            }

        } 
    }

Thanks for your help谢谢你的帮助

rand() returns an int , which is signed. rand()返回一个int ,它是有符号的。 english.size() returns a size_t , which is unsigned. english.size()返回一个size_t ,它是无符号的。 So, to compute the expression rand() % english.size() , the return value of rand() will need to be cast to an unsigned type.因此,要计算表达式rand() % english.size()rand()的返回值需要转换为unsigned类型。

This causes an implicit conversion from int to unsigned long , which is throwing this warning.这会导致从intunsigned long的隐式转换,从而引发此警告。

I'd strongly suggest looking into std::uniform_int_distribution , which you can specify a return type for.我强烈建议查看std::uniform_int_distribution ,您可以为其指定返回类型。 Here's a good example of how to use it.是如何使用它的一个很好的例子

暂无
暂无

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

相关问题 隐式转换将signedness&#39;int&#39;更改为&#39;unsigned int&#39; - implicit conversion changes signedness 'int“ to 'unsigned int” 当涉及隐式转换时,为什么三元运算符将(...?int:unsigned long long)转换为int? - Why does the ternary operator turn (… ? int : unsigned long long) into int when an implicit conversion is involved? 错误:从&#39;int(*)()&#39;到&#39;long unsigned int&#39;的无效转换 - error: invalid conversion from 'int (*)()' to 'long unsigned int' C++ - unsigned long long 到 signed long long 的隐式转换? - C++ - Implicit conversion of unsigned long long to signed long long? 如何解决隐式转换丢失整数精度:&#39;size_t&#39;(又名&#39;unsigned long&#39;)到&#39;int&#39;警告? - How to solve the Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' warning? 如何支持从 Variant 类型的隐式转换,例如从 int 到 unsigned long? - How to support implicit conversion from a Variant type, e.g. from int to unsigned long? iOS-隐式转换将整数精度&#39;size_t&#39;(aka&#39;unsigned long&#39;)转换为&#39;int&#39; - iOS - implicit conversion loses integer precision 'size_t' (aka 'unsigned long') to 'int' 没有从long unsigned int转换为long unsigned int& - No conversion from long unsigned int to long unsigned int& 是转换int - &gt; unsigned long long由标准定义 - Is conversion int -> unsigned long long defined by the standard 错误:&#39;18446744069414584320ull&#39;从&#39;long long unsigned int&#39;到&#39;int&#39;的缩小转换{} [-Wnarrowing] - error: narrowing conversion of ‘18446744069414584320ull’ from ‘long long unsigned int’ to ‘int’ inside { } [-Wnarrowing]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM