I've created a Url Encoder class whose job is to encode or decode an Url.
For storing the special characters I've use a map std::map<std::string, std::string> reserved
.
And I've initialized the map like this this->reserved["!"] = ":)";
For reading characters form a given string I'm using an iteratorfor(string::iterator it=input.begin(); it!=input.end(); ++it)
Now when I try to replace a special character using replace function encodeUrl.replace(position, 1, this->reserved[*it]);
I get the following error
Url.cpp: In member function ‘std::string Url::Url::UrlEncode(std::string)’:
Url.cpp:69:54: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/c++/4.6/bits/basic_string.tcc:214:5: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]’ [-fpermissive]
I'm not sure what's wrong with the code. Here's my function
string Url::UrlEncode(string input){
short position = 0;
string encodeUrl = input;
for(string::iterator it=input.begin(); it!=input.end(); ++it){
unsigned found = this->reservedChars.find(*it);
if(found != string::npos){
encodeUrl.replace(position, 1, this->reserved[*it]);
}
position++;
}
return encodeUrl;
}