【发布时间】:2015-01-27 12:17:25
【问题描述】:
我希望这将是一个非常基本的问题,这可能是由于我对很多 C++ Win32 API 实用程序缺乏了解。
无论如何,我在使用 sprintf() 函数时遇到了一些问题。我用这样的字符串没有问题:
//memory is a void pointer that maps a file to shared memory
sprintf((char *)memory, "Shared memory message"); //Write to shared memory
但是当我尝试使用 string 变量时它不起作用....
sprintf((char *)memory, str_var); //Write to shared memory
我收到一条错误消息:no suitable conversion function from "std::string" to "const char *" exists。我什至无法通过类型转换来解决这个问题,就像我对 memory 所做的那样。
这似乎很不一致。我做错了什么,我该如何给它一个它会接受的值?
【问题讨论】:
-
std::string 无法转换为 const char*,使用 .c_str() 成员方法转换
-
尝试学习字符串和 c 风格字符串的区别。 [stackoverflow.com/questions/10958437/…[1]:stackoverflow.com/questions/10958437/…
标签: c++ string shared-memory