简体   繁体   English

对于std :: string :: c_str,无法从'const char *'转换为'char *'

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

This gives the error: cannot convert from 'const char *' to 'char *'. 这给出了错误:无法从'const char *'转换为'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? 如何在不更改函数func的接口的情况下有效地消除此错误? The implementer of this interface: 该接口的实现者:

void (func*)(char **result) 

forgot to use const char** in the signature. 忘了在签名中使用const char **。 I can't change it. 我无法改变它。

Remember this is a mock and I'm only used in my unit tests. 记住这是一个模拟,我只在我的单元测试中使用。

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. 虽然这不能保证在C ++ 11之前工作,但是在大多数或所有当前编译器上都可以正常工作。

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. 更改个别字符应该没问题。

In test code, and if you are certain that the user of the interface isn't going to mutate the char*, maybe you could use a const_cast ? 在测试代​​码中,如果您确定接口的用户不会改变char *,那么您可以使用const_cast吗?

That assumes the caller doesn't take ownership of the char * ; 假设调用者不接受char *所有权; if that is the case, then you should make a copy. 如果是这种情况,那么你应该复制一份。

If you're absolutely certain that the interface function will not modify the string, you can use 如果您完全确定接口函数不会修改字符串,则可以使用

*result = const_cast<char *>((resultsI++)->c_str());

to remove const ness. 删除const

Otherwise, another option is to switch from using std::vector<std::string> to std::vector<std::vector<char>> but then you'll have to make sure you properly null terminate the strings. 否则,另一个选择是从使用std::vector<std::string>切换到std::vector<std::vector<char>>但是你必须确保正确地null终止字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 string和const char *和.c_str()? - string and const char* and .c_str()? 的std ::矢量 <std::basic_string<char> &gt; :: const_iterator&#39;没有名为&#39;c_str&#39;的成员 - std::vector<std::basic_string<char> >::const_iterator’ has no member named ‘c_str’ 使用`const_cast`将`std :: string :: c_str()`的结果传递给`mkdtemp()` <char*> ()` - Passing the results of `std::string::c_str()` to `mkdtemp()` using `const_cast<char*>()` 无法从const char []转换为std:string * - Cannot convert from const char[] to std:string * string :: c_str()和string :: data()无法转换为const char * - string::c_str() and string::data() not working for conversion to const char* 使用 c_str() 或 toCharArray() 将字符串转换为常量字符? - String to const char conversion using c_str() or toCharArray()? 你能安全地从它的 c_str() const char* 中获取一个指向字符串的指针吗? - Can you safely get a pointer to a string from its c_str() const char*? 如何设置std字符串(c_str())中的char *值不起作用 - how to set char * value from std string (c_str()) not working C ++无法将&#39;const char *&#39;转换为&#39;std :: string *&#39; - C++ cannot convert 'const char*' to 'std::string*' 无法将&#39;std :: string&#39;转换为&#39;const char * - Cannot convert ‘std::string’ to ‘const char*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM