質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

1447閲覧

C言語でクラスを作りたい。

32Bit_int

総合スコア22

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2020/01/13 19:01

編集2020/01/13 19:02

前提・実現したいこと

C言語で、オブジェクト指向のクラスのようなものを、マクロで作ろうとしているのですが、ガベージコレクション(みたいなもの)が、うまくできません。

該当のソースコード

C言語

1#include<studio.h> 2#define self void *cslf 3void *cslf; 4#define private(class_name) struct private_ ## class_name 5#define public struct 6#define classdef(class_name) typedef class_name* class_name; struct class_name instance_of_ ## class_name 7#define funcAttach(class_name) void init_ ## class_name(void) 8#define settings(class_name, parent_class) int myNumber; void(*delete)(void*); struct parent_class base; struct private_ ## class_name private 9#define dtor(class_name) void class_name ## __delete(void *cslf) 10#define new(class_name, args) classes[class_number] = calloc(sizeof(struct class_name), 1); do{if(class_number<510){memcpy(classes[class_number], &instance_of_ ## class_name, sizeof(struct class_name)); ((class_name)classes[class_number])->myNumber=class_number; cslf=classes[class_number]; class_name args; cslf=0; classes[class_number+1]=class_name ## __delete; class_number+=2; }else{free(classes[class_number]); classes[class_number]=0; }}while(0) 11#define setThis(class_name) do{class_name this=(class_name)cslf;}while(0) 12#define INIT(class_name, parent_class) instance_of_ ## class_name.delete=class_name ## __delete; instance_of_ ## _class_name=instance_of_ ## parent_class 13#define Delete(object) do{object->delete(object); classes[object->myNumber]=0; free(object); }while(0) 14typedef void *obj; 15int depth = -1; 16int class_number=0; 17void *classes[512][512]{{0}}; 18struct No_superclass{ 19} 20struct No_superclass instance_of_No_superclass; 21void delete_garbage(void){ 22 for(int i=0;i<class_number;i+=2){ 23 if(classes[depth][i]!=0){ 24 void(*dlt)(void*)= classes[depth][i+1]; 25 dlt(classes[depth][i]); 26 free(classes[depth][i]); 27 classes[depth][i]=0; 28 } 29 } 30 depth—; 31 class_number=0; 32} 33 34 35 36/* クラス定義 */ 37private(Human) { 38 int age; 39} public Human { 40 char name[64]; 41 int(*getAge)(obj); 42 void(*setAge)(obj, int); 43} 44dtor(Human) { 45 setThis(Human); 46 /* デストラクタ */ 47} 48void Human__setAge(self, int age) { 49 setThis(Human); 50 this->private.age=age; 51} 52int Human__getAge(self) { 53 setThis(Human); 54 return this->private.age; 55} 56void Human(void) { 57 setThis(Human); 58 /* コンストラクタ */ 59} 60 61int main(int argc, char** argv) { 62 /* ブロックの始めでdepthを+1 */ 63 depth++; 64 65 Human h = new(Human,()); 66 h.setAge(h, 57); 67 printf("This human is %d years old.¥n", h.getAge(h)); 68 69 /* ブロックの最後でガベージを削除 */ 70 delete_garbage(); 71}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/01/13 21:50

C# は関係ないですよね。タグを外してください。
y_waiwai

2020/01/13 22:39

で、しつもんはなんでしょうか
m.ts10806

2020/01/13 23:53

起きている問題は何でしょうか。 「うまくいかない」は誰にも何も伝えない表現なので避けてください。
guest

回答1

0

ベストアンサー

text

1usr ~/Project/test % cc t1.c | grep error 2t1.c:15:5: warning: no previous extern declaration for non-static variable 'depth' [-Wmissing-variable-declarations] 3int depth = -1; 4 ^ 5t1.c:15:1: note: declare 'static' if the variable is not intended to be used outside of this translation unit 6int depth = -1; 7^ 8t1.c:16:5: warning: no previous extern declaration for non-static variable 'class_number' [-Wmissing-variable-declarations] 9int class_number=0; 10 ^ 11t1.c:16:1: note: declare 'static' if the variable is not intended to be used outside of this translation unit 12int class_number=0; 13^ 14t1.c:17:24: error: expected ';' after top level declarator 15void *classes[512][512]{{0}}; 16 ^ 17 ; 18t1.c:18:1: warning: empty struct has size 0 in C, size 1 in C++ [-Wc++-compat] 19struct No_superclass{ 20^ 21t1.c:18:1: warning: empty struct is a GNU extension [-Wgnu-empty-struct] 22t1.c:19:2: error: expected ';' after struct 23} 24 ^ 25 ; 26t1.c:24:13: warning: initializing 'void (*)(void *)' with an expression of type 'void *' converts between void pointer and function pointer [-Wpedantic] 27 void(*dlt)(void*)= classes[depth][i+1]; 28 ^ ~~~~~~~~~~~~~~~~~~~ 29t1.c:26:7: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration] 30 free(classes[depth][i]); 31 ^ 32t1.c:30:8: error: non-ASCII characters are not allowed outside of literals and identifiers 33 depth—; 34 ^ 35t1.c:30:3: warning: expression result unused [-Wunused-value] 36 depth—; 37 ^~~~~ 38t1.c:21:6: warning: no previous prototype for function 'delete_garbage' [-Wmissing-prototypes] 39void delete_garbage(void){ 40 ^ 41t1.c:21:1: note: declare 'static' if the function is not intended to be used outside of this translation unit 42void delete_garbage(void){ 43^ 44static 45t1.c:39:2: error: expected ';' after struct 46} public Human { 47 ^ 48 ; 49t1.c:43:2: error: expected ';' after struct 50} 51 ^ 52 ; 53t1.c:44:1: warning: declaration shadows a variable in the global scope [-Wshadow] 54dtor(Human) { 55^ 56t1.c:9:60: note: expanded from macro 'dtor' 57#define dtor(class_name) void class_name ## __delete(void *cslf) 58 ^ 59t1.c:3:7: note: previous declaration is here 60void *cslf; 61 ^ 62t1.c:45:11: error: must use 'struct' tag to refer to type 'Human' 63 setThis(Human); 64 ^ 65 struct 66t1.c:11:32: note: expanded from macro 'setThis' 67#define setThis(class_name) do{class_name this=(class_name)cslf;}while(0) 68 ^ 69t1.c:45:11: error: use of undeclared identifier 'Human' 70t1.c:44:1: warning: unused parameter 'cslf' [-Wunused-parameter] 71dtor(Human) { 72^ 73t1.c:9:60: note: expanded from macro 'dtor' 74#define dtor(class_name) void class_name ## __delete(void *cslf) 75 ^ 76・・・Omit・・・ 77 78fatal error: too many errors emitted, stopping now [-ferror-limit=] 7920 warnings and 20 errors generated.

※Cを基礎から勉強し直して下さい。GCなんてまだ早いです。

投稿2020/01/14 01:51

編集2020/01/14 02:53
cateye

総合スコア6851

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cateye

2020/01/14 03:12 編集

色々有りますが、記述のおかしい所↓ int depth = -1; ・・・何故-1から? ・・・ void *classes[512][512]{{0}};・・・これは初期化ですか? ↑に付いては、lintも解析諦めたw usr ~/Project/test % lint t1.c Splint 3.1.2 --- 20 Feb 2018 Command Line: Setting +showcolumn redundant with current value t1.c:17:25: Parse Error: Non-function declaration: classes : void * [512][512]. (For help on parse errors, see splint -help parseerrors.) *** Cannot continue.
cateye

2020/01/14 09:44 編集

エラーチェック強めですが・・・エラーが多すぎます(コンパイラが文句言っているw) &多すぎて貼り付けられなかった^^; 環境はLinuxmit19.3+Clang-10 ・・・最近は、机上デバッグなんてしないんでしょうか?
cateye

2020/01/14 03:31 編集

(使える環境なら)-Eオプションを指定して、マクロの展開も確認しましょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問