函数中已签名/未签名的警告

warning of signed/unsigned in function

本文关键字:警告 函数      更新时间:2023-10-16

作为项目的一部分,我编写了一个函数来解析字符串参数。功能运行良好,但我收到一条警告,上面写着:C4018: '<' : signed/unsigned mismatch。我不知道为什么会发出这个警告。我能在这里得到一些帮助吗。谢谢

void FileMgr::parseCmdLineForTextSearch(std::string b)
{
    int count=0;
    int flag = 0;
    patternVector.clear(); 
    for (int i = 0; i < b.length(); i++)   // this line where
 // the warning line comes

{
            if (b[i] == '"')
            {
                count++;
            }
        }
        if (count == 2)
        {
            for (int i = 0; i < b.length(); i++)
            {
                if (b[i+1] == '"')
                {
                    flag = 1;
                    tmp = b.substr(0, i+1);
                    tmp.erase(0, 1);
                    break;
                }
                else
                {
                    continue;
                }
            }
            std::istringstream iss(b);
            std::string word;
            while (iss >> word)
            {                  // for each word in b
                if (word.find("*.") == 0)
                {        // if it starts with *.
                    patternVector.push_back(word); // add it
                }
            }
            if (patternVector.size() == 0)
            {
                patternVector.push_back("*.*");
            }
            isCorrect = true;
        }
        else
            isCorrect = false;
    }

b.length()返回无符号的size_t。在for循环中,您将比较有符号的int i和无符号的b.length(),这就是您看到警告的原因。若要消除它,请在使用i指示数组索引时使用size_t i而不是int i

无关:您在这里有一个越界访问if (b[i+1] == '"')

使用std::string::iterator可以避免unsignedsigned比较无意义。

for ( auto iter = b.start(); iter != b.end(); ++iter )