const_iterator not able to convert in char pointer

hi all,

I am trying to run a c++ code, which is a open source. And already tested visual studio 6 and .NET 2003. I am trying to run it on VS 2005. The code is this:-
1
2
3
4
5
6
7
8
9
10
bool OpenIPMPDOIContentInfoManager::ParseHostIPPort(const std::string& hostURL,
    std::string& hostIP, int& hostPort) {
  char* colon = strchr(hostURL.data(), ':');(1st error)
  if (colon == NULL) {
    return false;
  }
  hostIP = std::string(hostURL.begin(), std::string::const_iterator(colon));(2nd error)
  hostPort = atoi(++colon);
  return true;
}


And it's giving following error
1
2
3
4
5
6
7
8
9
10
error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
        Conversion loses qualifiers
error C2440: '<function-style-cast>' : cannot convert from '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 second error is coming because const_iterator is using as a constructor.

Can anybody suggest what's wrong with the code. Is it bcoz of compiler?
Thanks in advance
Last edited on
hostURL.data() gives you a const char* so the const version of strchr will be called, returning a const char*. Change the type of colon to const char*.

std::string::const_iteratorcolon are you trying to cast a pointer into a std::string::const_iterator? Don't do that. You can use pointers as iterators so this should work:
hostIP = std::string(hostURL.data(), colon);
Last edited on
thanks Peter for the reply.
I did what you have said. First error has gone. After changing hostip, now its giving following error

1
2
3
4
5
6
7
'std::basic_string<_Elem,_Traits,_Ax>::basic_string' : none of the 13 overloads could convert all the argument types
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
Last edited on
try hostURL.cbegin() instead on line 7, and hostPort=atoi(colon+1); on line 8.
Hi viliml, thanks for the reply.

I did what you have said. It's giving error for that too. Here is the code snippet:
1
2
3
4
5
6
7
8
9
10
11
12
bool OpenIPMPDOIContentInfoManager::ParseHostIPPort(const std::string& hostURL,
    std::string& hostIP, int& hostPort) {
  const char* colon = strchr(hostURL.data(), ':');
  if (colon == NULL) {
    return false;
  }
  
	//std::string::const_iterator p = *colon;
  hostIP = std::string(hostURL.cbegin(),colon);
  hostPort = atoi(colon+1);
  return true;
}


here is the error
1
2
3
4
5
6
7
error C2039: 'cbegin' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]


just wanted to mention that code was tested on visual 6 and .NET 2003. But i am running it on 2005 because of the unavailability of these two IDE.

I am not very much familiar with these type of c++ programming, but i feel that in 2005 const_iterators implementation has been changed. So it's not able to work on char pointer.

Thanks
Hi Peter and viliml,

I made following change in the code and now it's compiling, but i don't know, it's correct or not.
can you please check it.

1
2
3
4
5
6
7
8
9
10
11
12
bool OpenIPMPDOIContentInfoManager::ParseHostIPPort(const std::string& hostURL,
    std::string& hostIP, int& hostPort) {
  const char* colon = strchr(hostURL.data(), ':');
  if (colon == NULL) {
    return false;
  }
  
	std::string::iterator p;
	*p = *colon;
   hostIP = std::string(hostURL.begin(),p);
  hostPort = atoi(colon+1);
  return true; 


please give me suuggestion. Will this code work or not.
Thanks
No that code is not correct.
1
2
3
4
5
std::string::iterator p; // p is not referring to a valid element
*p = *colon;             // so you should not try to dereference it.
                         // This is similar to how normal pointers work:
                         // You should not dereference dangling
                         // pointers and null pointers. 
Last edited on
ohh, then please suggest me something. Because this code is compiling, that means the solution would be something like this only. Please suggest something.
I have already suggested something that should work but you said it didn't work. http://ideone.com/Zs6V7
hey Peter, thanks buddy.

It;s working now. cheers!!!!!

I made two changes, first i added two new headers
#include <cstring>
#include <cstdlib>

and then i change hostURL. begin() to hostURL.data().

Now it's working. Thanks alot. You did it. Thanks :)
Topic archived. No new replies allowed.