[英]Converting string to const* char
I have two string
declarations: 我有两个
string
声明:
killerName
victimName
I need to convert these two string values to const* char. 我需要将这两个字符串值转换为const * char。
Example of how I use my method: 我如何使用方法的示例:
if (killer.IsRealPlayer) {
killerName = killer.GetName(); -- need to convert to const* char
victimName = victim.GetName(); -- need to convert to const* char
Notice(killerName + "has slain:" + victimName, killer.GetMapIndex(), false);
}
Some error I receive: 我收到一些错误:
Error 111 error C2664: 'Notice' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char */
错误111错误C2664:'通知':无法将参数1从'std :: basic_string <_Elem,_Traits,_Ax>'转换为'const char * /
It seems that function Notice
have the first parameter of type const char *
However the expression passed to it as the first argument 似乎函数
Notice
具有类型为const char *
的第一个参数const char *
但是,表达式作为第一个参数传递给它
killerName + "has slain:" + victimName
has type std::string
具有类型
std::string
Simply call the function the following way 只需按以下方式调用该函数
Notice( ( killerName + "has slain:" + victimName ).c_str(), killer.GetMapIndex(), false);
Notice(string(killerName + "has slain:" + victimName).c_str(), killer.GetMapIndex(), false);
std::string::c_str()
gives the const char*
to the buffer. std::string::c_str()
将const char*
赋予缓冲区。 I think that's what you want. 我想这就是你想要的。
See: http://www.cplusplus.com/reference/string/string/c_str/ 参见: http : //www.cplusplus.com/reference/string/string/c_str/
As others already wrote, the result of killerName + "has slain:" + victimName
is of type std::string
. 正如其他人已经写过的,
killerName + "has slain:" + victimName
是std::string
类型的。 So, if your Notice()
function expects a const char*
as first parameter, you must convert from std::string
to const char*
, and since there is no implicit conversion defined for std::string
, you must call the std::string::c_str()
method: 因此,如果
Notice()
函数期望将const char*
作为第一个参数,则必须从std::string
转换为const char*
,并且由于没有为std::string
定义隐式转换,因此必须调用std::string::c_str()
方法:
Notice((killerName + "has slain:" + victimName).c_str(), killer.GetMapIndex(), false);
However, I'd like to ask: why do you have Notice()
expecting a const char*
as first parameter? 但是,我想问一个问题:为什么您要用
Notice()
期望将const char*
作为第一个参数?
Would it be better to just use const std::string&
? 只使用
const std::string&
会更好吗? In general, in modern C++ code, you may want to use string classes like std::string
instead of raw char*
pointers. 通常,在现代C ++代码中,您可能希望使用诸如
std::string
类的字符串类,而不是原始char*
指针。
(Another option would be to have two overloads of Notice()
: one expecting a const std::string&
as first parameter, and the other one expecting a const char*
, if for some reason the const char*
version does make sense in your particular context; this double overload pattern is used eg in the std::fstream
constructor.) (另一种选择是有两个Notification
Notice()
重载:一个重载const std::string&
作为第一个参数,另一个重载const char*
,如果由于某种原因const char*
版本确实对您有意义特定的上下文;例如在std::fstream
构造函数中使用此双重重载模式。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.