C++:<= 已签名和未签名之间的冲突
C++: <= conflict between signed and unsigned
我已经在.substr函数上创建了一个包装器:
wstring MidEx(wstring u, long uStartBased1, long uLenBased1)
{
//Extracts a substring. It is fail-safe. In case we read beyond the string, it will just read as much as it has
// For example when we read from the word HELLO , and we read from position 4, len 5000, it will just return LO
if (uStartBased1 > 0)
{
if (uStartBased1 <= u.size())
{
return u.substr(uStartBased1-1, uLenBased1);
}
}
return wstring(L"");
}
它可以正常工作,但是编译器给我警告"&lt; =签名和未签名之间的冲突"。
有人可以告诉我如何正确执行吗?
非常感谢!
您应该使用wstring::size_type
(或size_t
)而不是long
:
wstring MidEx(wstring u, wstring::size_type uStartBased1, wstring::size_type uLenBased1)
{
//Extracts a substring. It is fail-safe. In case we read beyond the string, it will just read as much as it has
// For example when we read from the word HELLO , and we read from position 4, len 5000, it will just return LO
if (uStartBased1 > 0)
{
if (uStartBased1 <= u.size())
{
return u.substr(uStartBased1-1, uLenBased1);
}
}
return wstring(L"");
}
这是u.size()
的确切返回类型。这样,您确保比较给出了预期的结果。
如果您使用std::wstring
或其他标准库容器(例如std::vector
等),则x::size_type
将定义为size_t
。因此,使用它将更加一致。
您想要unsigned
参数,类似:
wstring MidEx(wstring u, unsigned long uStartBased1, unsigned long uLenBased1)
相关文章:
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- 库之间的冲突
- 如何避免 Cocos2d-x 3.15.1 中的 MenuItemImage 和 MenuItemSprite 之间的冲突
- 包括两个文件 C++ 之间的冲突
- 如何解决两个不使用命名空间的第三方库之间的类名冲突?
- 2 个 QGraphicsPixmapItems 之间的 QT 冲突检测
- 字符串和整数之间的模板冲突
- 两个提升版本之间的冲突
- C++编译错误是由于使用 std::move 时运动构造函数与其他非运动构造函数之间的冲突
- 调用 ADL 时,表达式和命名空间之间是否会发生冲突
- 复制构造函数和模板构造函数之间的冲突
- 名称名称空间和类模板之间的冲突:不同的编译器行为
- 如何解决GMP的32位和64位库之间的冲突
- OpenCV和Tesseract之间是否存在任何冲突
- 最新版本的提升和提升几何库 (GGL) 之间的冲突
- 包含:stdafx.h 和 <vector> VisualStudio 2012 C++ 之间的冲突(错误 C2953:已定义类模板)
- 类名和枚举值之间的冲突:无需命名空间即可解析
- C Socket 和 Omnet++ 之间的方法名称冲突
- 在类层次结构中,完美的转发构造函数和复制构造函数之间的冲突