Reputation:
I am trying to split a string on .
in C++ and then the first splitted string I need to pass into another method which accepts const char* key
.. But everytime I do, I always get an exception -
Below is my code -
istringstream iss(key);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, '.')) {
if (!token.empty()) {
tokens.push_back(token);
}
}
cout<<"First Splitted String: " <<tokens[0] << endl;
attr_map.upsert(tokens[0]); //this throws an exception
}
Below is the upsert method in AttributeMap.hh files -
bool upsert(const char* key);
And below is the exception I always get -
no matching function for call to AttributeMap::upsert(std::basic_string<char>&)
Is there anything I am missing?
Upvotes: 0
Views: 6889
Reputation: 23624
You should use string::c_str
attr_map.upsert(tokens[0].c_str())
//^^^
You can check the reference for details on c_str()
function.
You are getting the error because upsert
function expects const char*
, but you are passing std::string
, type mismatch.
Upvotes: 0
Reputation: 33046
Use c_str()
to get a pointer to a "null-terminated character array with data equivalent to those stored in the string" (quoting from the documentation).
attr_map.upsert(tokens[0].c_str()); //this won't throw an exception
Upvotes: 2