cannot convert from 'const char *' to 'char *' for std::string::c_str

c++

Solution

Try:

*result = &(*resultsI++)[0];

Although this isn't guaranteed to work prior to C++11 it is known to be OK on most or all current compilers.

The danger is that if the function tries to change the length of the string, you could get some nasty errors. Changing individual characters should be OK.

Problem

This gives the error: cannot convert from 'const char *' to 'char *'. ``` class Mock { public: ... static void func(char **result) { *result = (resultsI++)->c_str(); } static std::vector<std::string> results; static std::vector<std::string>::iterator resultsI; }; std::vector<std::string> Mock::results; std::vector<std::string>::iterator Mock::resultsI; ``` How can I validly get rid of this error without changing the interface to the function func? The implementer of this interface: ``` void (func*)(char **result) ``` forgot to use const char** in the signature. I can't change it. Remember this is a mock and I'm only used in my unit tests.

Original source