gpt4 book ai didi

c++ - 无法从 'const char *' 转换为 'std::_String_const_iterator<_Elem,_Traits,_Alloc>

转载 作者:行者123 更新时间:2023-11-28 03:37:25 26 4
gpt4 key购买 nike

我在 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 风格的字符串函数 strchr 与 C++ 风格的 std::string 混合使用,因为它们不能很好地混合。使用 std::string::find 找到 : 然后使用 std::string::substr 创建 hostIP 否则,使用 std::find(算法)将迭代器放入字符串中的 : 并使用您当前的构造函数。

关于c++ - 无法从 'const char *' 转换为 'std::_String_const_iterator<_Elem,_Traits,_Alloc>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10449657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com