-1

I need some help from an error generated by the compiler.

I hope you can help me.

This is the error message:

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

And here is the code :

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;
}

The default compilator is freebsd ( GCC )

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Cosmin Iovan
  • 23
  • 1
  • 9

1 Answers1

3

This is the culprit

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

Try

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

The int type is signed, the size_t type (returned from .size()) is unsigned.

The same warning will occur in your next for loop

(int idx = 0; idx < vec_probs.size(); idx++)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218