string変数にcharポインタとchar constの加算

解決


ぺぺ  2005-11-04 20:02:34  No: 59475

ぺぺです。C++の初心者なので、いろいろ困っています。
どなたかご教授ください。よろしくお願いします。

OS:Linux Enterprise AS 4.0

string変数に、charポインタとconst charを加算した値を代入すると、コンパイルエラーが発生します。このプログラムって無効なのでしょうか?

char* a="abc";
string b;
b = "def" + a;   // コンパイルエラー

↓エラー内容
invalid operands of types `const char[4]' and `char*' to binary `operator+'


Blue  2005-11-04 20:07:20  No: 59476

> 無効なのでしょうか
です。
b = std::string( "def" ) + a;
または
b = "def";
b += a;
として、一度std::stringに入れれば、std::stringのoperator+でできます。


ぺぺ  2005-11-04 20:17:41  No: 59477

Buleさんありがとうございます。m(_ _)m
解決いたしました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加