Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;字符串/字符*串联_C++_String_Char_Concatenation - Fatal编程技术网

C++ C++;字符串/字符*串联

C++ C++;字符串/字符*串联,c++,string,char,concatenation,C++,String,Char,Concatenation,我编写了一个小函数来接受一个“字符串”并记录它 void IPC::bomb (char * msg) { /* etc */ } 此调用实例未编译: bomb( __FUNCTION__": socket() call failed: " + strerror(errno)); 诊断: ./c/IPC.cpp:26: error: expected ')' before string constant ./c/IPC.cpp:26: error: invalid conversion f

我编写了一个小函数来接受一个“字符串”并记录它

void
IPC::bomb (char * msg) {  /* etc */ }
此调用实例未编译:

bomb( __FUNCTION__": socket() call failed: " + strerror(errno));
诊断:

./c/IPC.cpp:26: error: expected ')' before string constant
./c/IPC.cpp:26: error: invalid conversion from 'const char*' to 'char*'
我很困惑如何有效地处理引用的文本、std::string、char*,以及常量是如何体现在其中的。如何解决上述问题?一般来说,字符串连接有一些经验法则吗,特别是在混合char*和字符串时

更新1:可能存在另一个问题:C预处理器显示此扩展:

bomb( std::string(__FUNCTION__ ": socket() call failed: ") + strerror((*__errno_location ())));

编辑:我不能简单地将
\uuu函数\uuu
中的“somestring”放在一起连接起来。我正在使用mingw编译器

为了安全起见,将所有三个部分连接在一起,如下所示:

bomb( string(__FUNCTION__) + ": socket() call failed: " + string(strerror(errno)) );

void IPC::bomb(char*msg)
更改为
void IPC::bomb(const char*msg)
,以消除第二个错误,这样它就可以接受受保护的常量字符串,不受任何修改。

编辑:我不能简单地通过将它们放在一起来连接
\u函数
“somestring”。我正在使用mingw编译器

为了安全起见,将所有三个部分连接在一起,如下所示:

bomb( string(__FUNCTION__) + ": socket() call failed: " + string(strerror(errno)) );

void IPC::bomb(char*msg)
更改为
void IPC::bomb(const char*msg)
,以消除第二个错误,这样它就可以接受受保护的常量字符串,使其不受任何修改。

不能将c字符串(
char*
字符串)与
+
运算符连接起来(除非您要编写重载运算符来执行此操作)。您必须创建一个足够长的新字符串,以包含要组合的两个字符串,然后使用
memcpy
strncpy
或类似的方法手动复制它们。

您不能连接c字符串(
char*
字符串)使用
+
运算符(除非要编写重载运算符)。您必须创建一个足够长的新字符串,以包含要组合的两个字符串,然后使用
memcpy
strncpy
或类似的方法手动复制它们。

您的函数签名设计为接受
char*
。但是,值
“string1”+…
是一个
const char*
,因为它是一个临时对象。将函数签名更改为
IPC::bomb(const char*msg)
,应该可以

顺便说一句,您不能仅仅使用运算符+连接两个C样式的字符串,因为它只执行指针运算。请尝试使用
std::string
,将所有字符串强制转换为该类型

所以,这个例子看起来像

void IPC::bomb (const std::string& msg) {  /* etc */ }

bomb(std::string(__FUNCTION__": socket() call failed: ") + 
     std::string(strerror(errno)));

如果您使用字符串。

您的函数签名被设计为接受
char*
。但是,值
“string1”+…
是一个
const char*
,因为它是一个临时对象。将函数签名更改为
IPC::bomb(const char*msg)
,应该可以

顺便说一句,您不能仅仅使用运算符+连接两个C样式的字符串,因为它只执行指针运算。请尝试使用
std::string
,将所有字符串强制转换为该类型

所以,这个例子看起来像

void IPC::bomb (const std::string& msg) {  /* etc */ }

bomb(std::string(__FUNCTION__": socket() call failed: ") + 
     std::string(strerror(errno)));

如果您使用字符串。

如果您将
IPC::bomb
更改为
IPC::bomb(const std::string&msg)
,则可以执行以下操作:

bomb(std::string(__FUNCTION__) + ": socket() call failed: " + strerror(errno));
而且没有任何错误

下面是一个完成类似操作的完整程序:

#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;
}
#包括
#包括
#包括
void func(std::string str)
{

std::cout如果您将
IPC::bomb
更改为
IPC::bomb(const std::string&msg)
,则可以执行以下操作:

bomb(std::string(__FUNCTION__) + ": socket() call failed: " + strerror(errno));
而且没有任何错误

下面是一个完成类似操作的完整程序:

#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;
}
#包括
#包括
#包括
void func(std::string str)
{

std::我试过了吗(在
\uuuu FUNCTION\uuuu
“:etc…”之间插入一个空格后?
但是现在我得到了
/c/IPC.cpp:26:error:error:expected primary expression在字符串常量之前的“('token./c/IPC.cpp:26:error:expected')。c预处理器显示了这个扩展:
bomb(std::string(\uu FUNCTION\uu):socket()调用失败:“)+strerror((*.\u errno\u location()));
好的,我已经编辑并添加了一个完整的小程序来尝试。好的,我想我没有注意到您在std::string()中单独包装了
函数,然后连接了“:somestring”。我还在尝试
字符串(\uu函数:somestring”)
语法。通过使用您的方法,它现在可以工作了(或者至少可以编译!)。谢谢。我在添加演示程序时更改了它,但我不太明白为什么它会有不同……我想我读到,
\uuuuuu函数\uuuu
不是宏。这可能是原因吗?我试过了(在
\uuuu FUNCTION\uuuu
“:etc…”之间插入空格后,
/c/IPC.cpp:26:error:expected primary expression在字符串常量之前的“('token./c/IPC.cpp:26:error:expected')。c预处理器显示此扩展:
bomb(std::string(\uu FUNCTION\uuuuuuu):socket()调用失败:“)+strerror((*.\u errno\u location());
好的,我已经编辑并添加了一个完整的小程序来试用。好的,我想我没有注意到您在std::string()中单独包装了
\uu函数\uuuuu
,然后连接了“:somestring”。我仍然在尝试
字符串(\uu函数\uuuuuu::somestring”)
语法。通过使用您的方法,它现在可以工作了(或者至少可以编译!)。谢谢。作为添加演示程序的一部分,我改变了这一点,但我不太明白为什么会有不同……我想我读到,
\uuuuuuuuuuu函数
不是宏。这可能是原因吗?是的。函数实际上是一个变量!!!
\uuuuuuu函数
我的意思是。是。是的。函数实际上是一个变量!!!
\uuuuuuuuuu函数
我是说。