简体   繁体   中英

cannot convert from 'const char *' to 'std::_String_const_iterator<_Elem,_Traits,_Alloc>

I am running Openipmp client on windows on MS visual studio 2005. Though according to document it was tested only on visual studio 6 and MS visual studio .NET.

when i am compiling DRMPlugin, one piece of code giving error

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

Here is the code

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;
}

Can somebody tell me what is wrong with the code.

Please help.

hostIP = std::string(hostURL.begin(), std::string::const_iterator(colon));

You cannot create a std::string::const_iterator from a pointer to a character. You should not try to mix C style string functions strchr with C++ style std::string as they don't mix well. Use std::string::find to locate the : and then std::string::substr to create the hostIP or else, use std::find (algorithm) to get an iterator into the : inside the string and use your current constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM