「猫でも分かるプログラミング」というサイトを使って勉強しているものです。
添付したリンク先のコードを記述しコンパイルするとエラーをはいてしまいます。
エラー内容及び解消方法についてご教示ください。
「char」として宣言している部分を「const char」として書き換えれば上手くいくかと思い
リンク先のコードから多少変更しておりますが、いまだうまくいっていない状態でございます。
(変更後のほうがエラーの数は減りましたが。。。)
下記コードを入力し「sample.cpp」で保存
↓
PowerShell上で「g++ sample.cpp」と入力し実行
↓
するとエラーをはいてしまいます。
【参照にしたサイト】
http://www.kumei.ne.jp/c_lang/cpp/cpp_09.htm
【コード】
C++
1#include <stdio.h> 2#include <string.h> 3 4class Cat{ 5 int leg; 6 char body_siza[32]; 7 char kind_of_cat[32]; 8public: 9 char cat_name[32]; 10 Cat(char *kind); 11 12 void show(const char *name); 13}; 14 15Cat::Cat(char *kind) 16{ 17 strcpy(kind_of_cat,kind); 18 leg=4; 19 if(!strcpy(kind,"メイン・クーン")) 20 strcpy(body_siza,"大型"); 21 else{ 22 if(!strcpy(kind,"アメリカン・ショート・ヘア")) 23 strcpy(body_siza,"小型"); 24 else 25 strcpy(body_siza,"不明"); 26 } 27} 28 29inline void Cat::show(const char *name) 30{ 31 printf("%sは、%sという種類の猫で足は%d本あります。\n" 32 "また、この猫の体型は%sです。\n",name,kind_of_cat,leg,body_siza); 33 return; 34} 35 36int main(void) 37{ 38 Cat MaineCoon("メイン・クイーン"); 39 Cat American_Short("アメリカン・ショート・ヘア"); 40 Cat House_Hold("家庭猫"); 41 MaineCoon.show("マイケル"); 42 American_Short.show("ポチ"); 43 House_Hold.show("タマ"); 44 return 0; 45 46}
【エラー内容】
sample.cpp: In function 'int main()':
sample.cpp:38:37: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Cat MaineCoon("???C?“?E?N?C?[?“");
^
sample.cpp:39:52: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Cat American_Short("?A?????J?“?E?V?‡?[?g?E?w?A");
^
sample.cpp:40:28: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Cat House_Hold("‰A’e”L");
^

回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/17 12:46