函数中已签名/未签名的警告
warning of signed/unsigned in function
作为项目的一部分,我编写了一个函数来解析字符串参数。功能运行良好,但我收到一条警告,上面写着: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
可以避免unsigned
和signed
比较无意义。
for ( auto iter = b.start(); iter != b.end(); ++iter )
相关文章:
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 为什么我会收到警告,指出函数已使用但未定义,以及已定义但未使用?
- 我在 C++ 代码中遇到错误警告:控制到达非空函数 [-Wreturn 类型] 的末尾
- 返回非 void 的函数中没有返回语句警告
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 从多个模板化基类派生时出现"隐藏重载的虚函数"警告
- 当 noexcept 函数尝试在 gcc 或 clang 中调用非 noexcept 函数时启用警告
- 使用 bool 和 const char 重载的 C++ 函数会在没有警告的情况下产生歧义 (MSVC2012)
- 如何禁止在 g++ 中使用但从未定义警告的内联函数
- 删除显式默认函数声明时的警告
- C++未使用的变量警告,即使我在函数结束时返回它
- 在非constexpr函数上添加的constexpr限定符不会触发任何警告
- 宏函数调用缺少参数警告,即使给定了参数
- 如何在类成员函数中获得类似printf中的编译警告
- ioroutin.c:67:4:警告:函数 ' ' 的隐式声明在 C99 中无效
- 如何警告函数声明和函数定义中参数名称的差异
- 警告:函数将始终返回True
- 警告:函数返回与C不兼容的用户定义类型
- C中的C++函数给出警告:函数的隐式声明