[英]C++ string/char* concatenation
I wrote a small function to accept a "string" and log it. 我编写了一个小函数来接受“字符串”并记录它。
void
IPC::bomb (char * msg) { /* etc */ }
This instance of calling it doesn't compile: 此调用实例无法编译:
bomb( __FUNCTION__": socket() call failed: " + strerror(errno));
Diagnostics: 诊断:
./c/IPC.cpp:26: error: expected ')' before string constant
./c/IPC.cpp:26: error: invalid conversion from 'const char*' to 'char*'
I'm very confused about how to work effectively with quoted literals, std::string, char*, and how const-ness figures into it. 我对如何有效使用带引号的文字,std :: string,char *以及如何将const-ness引入其中感到困惑。 How do I solve the above problem?
我该如何解决以上问题? And in general, are there some rules of thumb about string concatenation, esp.
通常,关于字符串连接,尤其是有一些经验法则。 when mixing char* and strings?
当混合char *和字符串时?
UPDATE 1 : there may be another issue at work: the C preprocessor shows this expansion: 更新1 :可能还有另一个问题在起作用:C预处理器显示了此扩展:
bomb( std::string(__FUNCTION__ ": socket() call failed: ") + strerror((*__errno_location ())));
Your function signature is designed to accept char*
. 您的函数签名旨在接受
char*
。 However, the value "string1" + ...
is a const char*
, because it is a temporary object. 但是,值
"string1" + ...
是const char*
,因为它是一个临时对象。 Change your function signature to IPC::bomb(const char* msg)
, and it should be OK. 将函数签名更改为
IPC::bomb(const char* msg)
,应该可以。
By the way, you cannot just concatenate two C-style strings using operator+, because it just performs pointer arithmetic. 顺便说一句,您不能仅使用operator +连接两个C样式的字符串,因为它仅执行指针算术。 Try using
std::string
instead, casting every string you have to this type. 尝试改用
std::string
,将每个字符串强制转换为该类型。
So, this example could look like 所以,这个例子看起来像
void IPC::bomb (const std::string& msg) { /* etc */ }
bomb(std::string(__FUNCTION__": socket() call failed: ") +
std::string(strerror(errno)));
if you use strings. 如果您使用字符串。
Edit: i could not simply concatenate __FUNCTION__
"somestring" by placing them together. 编辑:我不能简单地通过将它们放在一起来连接
__FUNCTION__
“ somestring”。 I was using mingw compiler. 我正在使用mingw编译器。
So to be safe, concatenate all three parts together like this: 为了安全起见,将这三个部分串联在一起,如下所示:
bomb( string(__FUNCTION__) + ": socket() call failed: " + string(strerror(errno)) );
Change void IPC::bomb (char * msg)
into void IPC::bomb (const char * msg)
to get rid of the second error so it can accept constant string that are to be protected from any modification. 将
void IPC::bomb (char * msg)
更改为void IPC::bomb (const char * msg)
以摆脱第二个错误,以便它可以接受要进行任何修改保护的常量字符串。
You cannot concatenate c-strings ( char*
strings) with the +
operator (unless you were to write an overloaded operator to do so). 您不能用
+
运算符连接c字符串( char*
字符串)(除非您要编写一个重载的运算符来这样做)。 You'll have to create a new character string long enough to contain the two that you want to combine and then manually copy them over with memcpy
, strncpy
or something similar. 您必须创建一个足够长的新字符串,以包含要合并的两个字符串,然后使用
memcpy
, strncpy
或类似名称手动将其复制。
If you change IPC::bomb
so that it is IPC::bomb(const std::string& msg)
, then you could do: 如果将
IPC::bomb
更改为IPC::bomb(const std::string& msg)
,则可以执行以下操作:
bomb(std::string(__FUNCTION__) + ": socket() call failed: " + strerror(errno));
and not have any errors. 并且没有任何错误。
Here's a complete program to do somehting similar: 这是一个完成类似任务的完整程序:
#include <string>
#include <iostream>
#include <cstring>
void func(std::string str)
{
std::cout << str << std::endl;
}
int main() {
func(std::string(__FUNCTION__) + ":Some string " + strerror(2));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.