无法从 'const char *' 转换为 'std::_String_const_iterator<_Elem,_Traits,_Alloc>
cannot convert from 'const char *' to 'std::_String_const_iterator<_Elem,_Traits,_Alloc>
我在MS Visual Studio 2005的Windows上运行Openipmp客户端。虽然根据文档,它只在Visual Studio 6和MS visual studio .NET上进行了测试。
当我编译DRMPlugin时,一段代码给出错误
error C2440: '<function-style-cast>' : cannot convert from 'const char *'
to 'std::_String_const_iterator<_Elem,_Traits,_Alloc>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
这是代码
bool OpenIPMPDOIContentInfoManager::ParseHostIPPort(const std::string& hostURL,
std::string& hostIP, int& hostPort) {
const char* colon = strchr(hostURL.data(), ':');
if (colon == NULL) {
return false;
}
hostIP = std::string(hostURL.begin(), std::string::const_iterator(colon));
hostPort = atoi(++colon);
return true;
}
有人可以告诉我代码有什么问题吗?
请帮忙。
hostIP = std::string(hostURL.begin(), std::string::const_iterator(colon));
不能从指向字符的指针创建std::string::const_iterator
。您不应该尝试将 C 样式字符串函数与C++样式std::string
混合strchr
因为它们不能很好地混合。使用 std::string::find
找到:
,然后std::string::substr
创建hostIP
,否则,使用 std::find
(算法)将迭代器放入字符串内的:
并使用当前的构造函数。