模板 类型转换 C++

来源:百度知道 编辑:UC知道 时间:2024/05/23 21:09:30
下面的代码在vc6中可以编译,但是在vs2005中无法编译:
IXMLElement* XMLElement::AddChildElement(const std::string& path) {
char* dot = strchr((char *)path.data(), '.');
if (dot != NULL) {
IXMLElement* elem = AddChildElement(std::string(path.begin(), std::string::const_iterator(dot)));
if (elem == 0) {
elemLogger.UpdateLog(GetName() + "::AddChildElement: cannot add node with path " + path + "...");
return 0;
}
return elem->AddChildElement(std::string(std::string::const_iterator(++dot), path.end()));
}

报错为:
error C2440: “<function-style-cast>”: 无法从“char *”转换为“std::_String_const_iterator<_Elem,_Traits,_Alloc>”
2> with
2> [
2> _Elem=char,
2> _Traits=std::char_traits<char>,
2> _Alloc=std::allocator<char>
2> ]
2> 无构造函数可以接受源类型,或构造函数重载决策不明确
2&

IXMLElement* elem = AddChildElement(std::string(path.begin(), std::string::const_iterator(dot)));

问题是出在这句吧。
既然是string,你直接用path.find('.')就可以返回第一个指向'.'的迭代器了啊,当然,find还有重载的函数,主要是指定范围。

IXMLElement* elem = AddChildElement(std::string(path.begin(),path.find('.')));
应该就可以了。

-----------------------------------------------------------------
05这种出错信息看着真累。。。你还是把出错的那行代码贴出来吧,看样子好像是没有匹配类型~~

看不懂啊