我寫了一個小函數來接受一個「字符串」並記錄下來。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 from 'const char*' to 'char*'
我有關如何使用引用的文字,標準有效地開展工作很迷茫::字符串,char *以及常量如何計算。我如何解決上述問題?一般來說,是否存在關於字符串連接的一些經驗法則,尤其是,混合char *和字符串?
更新1:有可能在工作的另一個問題:C預處理表明這種擴張:
bomb(std::string(__FUNCTION__ ": socket() call failed: ") + strerror((*__errno_location())));
我試過了(在'__FUNCTION__'和'「之間插入一個空格之後:」etc ..「',但現在變成了'./c/IPC.cpp:26:錯誤:預期的主表達式'('token 。 /c/IPC.cpp:26:error:expected')'before string constant'。c預處理器顯示了這個擴展:'bomb(std :: string(__ FUNCTION__「:socket()call failed:」)+ strerror(( * __ errno_location())));' – Chap
嗯,讓我試試吧...... –
好的,我已經編輯並添加了一個完整的小程序來試用它 –