C++
- voluntas
- @ignis_fatuus
- ブン
- @Linda_pp
- 清楚なC++メイドBOT
- @tzik_tack
- 長谷川一輝
- wraith13
- @jj1bdx
- @cpp_akira
- 安藤敏彦
- @srz_zumix
- Siv3D
- takezoh
- まろ
- @okdshin
- @hnokx
- @ishidakei
- @take_cheeze
- TAKEI Yuya
- @mumumu
- I (@wx257osn2)
- Tommy6
- @tyottyoworks
- ___shanon
- わたやん
- @KorekaraSEDB
- @kariya_mitsuru
- @ciniml
- @beam2d
- @grafi_tt
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
51
1
2
3
4
5
6
7
using namespace std;8
9
class samp {10
char *s;11
public:12
samp() { cout << "a"; s = '\0'; }13
~samp() { if (s) free(s); cout << "sを開放する\n"; }14
void show() { cout << s << "\n"; }15
void set(char* str);16
};17
18
void samp::set(char* str) {19
cout << __LINE__ << ":" << str << endl;20
s = (char*)malloc(strlen(str) + 1);21
if (!s) {22
cout << "メモリ割り当てエラー\n";23
exit(1);24
}25
26
strcpy(s, str);27
28
}29
30
void input( samp& ob ) {31
char s[80];32
cout << "文字列の入力: ";33
cin >> s;34
cout << __LINE__ << ":" << s << endl;35
36
ob.set(s); 37
}38
39
int main()40
{41
samp ob;42
43
input( ob );44
ob.show();45
46
return 0;47
}48
49
50
// end.51
$ g++ prog.cc -Wall -Wextra
Stdin
abcStart
prog.cc: In constructor 'samp::samp()':
prog.cc:12:31: error: invalid conversion from 'char' to 'char*' [-fpermissive]
12 | samp() { cout << "a"; s = '\0'; }
| ^~~~
| |
| char
1
Finish