C++NOW:有符号和无符号整数表达式(Freebsd、GCC、Gmake)之间的比较

C++ NOW: comparison between signed and unsigned integer expressions (Freebsd, GCC, Gmake )

本文关键字:Gmake GCC 之间 比较 Freebsd 符号 表达式 无符号整数 C++NOW      更新时间:2023-10-16

我需要编译器生成的错误的帮助。

我希望你能帮助我。

这是错误消息:

FILE.cpp: In function 'int Gamble(std::vector<float, std::allocator<float> >&)':
FILE.cpp::6 :warning: comparison between signed and unsigned integer expressions
FILE.cpp:10: warning: comparison between signed and unsigned integer expressions

这是代码:

typedef std::vector <std::string> TTokenVector;
int Gamble(std::vector<float>& vec_probs)
{
    float range = 0.f;
    for (int i = 0; i < vec_probs.size(); i++)    //Line  6
}
    float fProb = fnumber(0.f, range);
    float sum = 0.f;
    for (int idx = 0; idx < vec_probs.size(); idx++)   //LINE  10
{
        sum += vec_probs[idx];
        if (sum >= fProb)
            return idx;
}
    return -1;
}

默认编译程序是freebsd(GCC)

这是的罪魁祸首

for (int i = 0; i < vec_probs.size(); i++)  

尝试

for (size_t i = 0; i < vec_probs.size(); i++)  

int类型是有符号的,size_t类型(从.size()返回)是无符号的。

相同的警告将出现在您的下一个for循环中

(int idx = 0; idx < vec_probs.size(); idx++)