Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

Affje

Treue Seele

  • »Affje« ist der Autor dieses Themas

Beiträge: 89

Beruf: Student

  • Private Nachricht senden

11

19.03.2011, 13:09

Ich poste einfach den kompletten Code. Das große Problem für viele wird wohl sein, dass unser Prof. - da er gerade auf Java bzw. generell OOP hinarbeitet - die Variable this benutzt, was natürlich in C++/Java ein Schlüsselwort ist :D

Mit einem C-COmpiler gibts keine Probleme, zur Not Suchen und ersetzen :D

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdlib.h> /* malloc, exit, NULL */
#include <stdio.h>  /* printf */

typedef const char* String;
typedef struct Account* Account;

long next_number = 1;   /* nächste zu vergebende Kontonummer */

/* Konto. */
struct Account {
  long number;      /* Kontonummer. */
  String holder;    /* Kontoinhaber. */
  long balance;     /* Kontostand in Cent. */
};

/* Gebührenpflichtiges Konto */
struct ChargedAccount {
    struct Account Account;  /* Property-Vererbung struct Account*/
    int count;
};

/* Dynamisches Objekt der Größe n Byte erzeugen. */
void* new_object (int n) {
  void* this = malloc(n);
  if (this == NULL) {
    printf("new_object: out of memory\n");
    exit(1);
  }
  return this;
}

/* Konto mit Inhaber h erzeugen. */
struct Account* new_account (String h) {
  struct Account* this = new_object(sizeof(struct Account));
  this->number = next_number++;
  this->holder = h;
  this->balance = 0;
  return this;
}

/* gebührenpflichtiges Konto initialisieren */
void init_charged_Account (struct ChargedAccount* this ,String h){
    this->Account.number = next_number++;
    this->Account.holder = h;
    this->Account.balance = 0;
}

/* Neues gebührenpflichtiges Konto */
struct ChargedAccount* new_charged_account (String h){
    struct ChargedAccount* this = new_object(sizeof(struct ChargedAccount));
    init_charged_Account(this, h);
    return this;
}

/* Abhebung für gebührenpflichtige Konten */
void withdraw_ChargedAccount (struct ChargedAccount* this, long amount) {
    withdraw(&(this->Account), amount);
    this->count++;
}

/* Einzahlung für gebührenpflichtige Konten */
void deposit_ChargedAccount (struct ChargedAccount* this, long amount) {
    deposit(&(this->Account), amount);
    this->count++;
}

/* Nummer von Konto this liefern. */
long number (struct Account* this) {
  return this->number;
}

/* Inhaber von Konto this liefern. */
String holder (struct Account* this) {
  return this->holder;
}

/* Kontostand von Konto this liefern. */
long balance (struct Account* this) {
  return this->balance;
}

/* Betrag amount auf Konto this einzahlen. */
void deposit (struct Account* this, long amount) {
  this->balance += amount;
}

/* Betrag amount von Konto this abheben. */
void withdraw (struct Account* this, long amount) {
  this->balance -= amount;
}

/* Betrag amount von Konto this auf Konto dest �berweisen. */
void transfer (struct Account* this, long amount,
                    struct Account* dest) {
  withdraw(this, amount);
  deposit(dest, amount);
}

/* Zeiger auf Kontoobjekt. */

int main () {
  /* Zwei Konten erzeugen. */
  Account a = new_account("Hans Maier");
  Account b = new_account("Fritz Müller");

  /* 1000 Cent auf Konto a einzahlen, */
  /* dann 300 Cent auf Konto b �berweisen. */
  deposit(a, 1000);
  transfer(a, 300, b);

  /* Kontodaten ausgeben. */
  printf("Konto a: %ld, %s, %ld\n",
   number(a), holder(a), balance(a));
  printf("Konto b: %ld, %s, %ld\n",
   number(b), holder(b), balance(b));

  return 0;
}

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

12

19.03.2011, 13:28

Die Fehlermeldungen inklusive Zeilenangaben würden die Suche auch beschleunigen.

Sollte C nicht ohnehin meckern schon beim Aufruf von Methoden (bzw. Verwendung von Bezeichnern) [Vergleich Zeile 57 und 88], die erst später deklariert werden? Oder hat sich da irgendwas geändert seit vor 10 Jahren?
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

Affje

Treue Seele

  • »Affje« ist der Autor dieses Themas

Beiträge: 89

Beruf: Student

  • Private Nachricht senden

13

19.03.2011, 13:30

Mein Fehler, sorry.

main.c:83: error: conflicting types for 'deposit'
main.c:63: error: previous implicit declaration of 'deposit' was here
main.c:88: error: conflicting types for 'withdraw'
main.c:57: error: previous implicit declaration of 'withdraw' was here

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

14

19.03.2011, 13:31

Irgendwie passen die Fehler nicht zu den Zeilennummern.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

Affje

Treue Seele

  • »Affje« ist der Autor dieses Themas

Beiträge: 89

Beruf: Student

  • Private Nachricht senden

15

19.03.2011, 13:34

Jetzt sollte es passen.

drakon

Supermoderator

Beiträge: 6 513

Wohnort: Schweiz

Beruf: Entrepreneur

  • Private Nachricht senden

16

19.03.2011, 13:40

Den Bezeichner gleich wie seine Typen zu nennen ist nie eine gute Idee.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

17

19.03.2011, 13:42

Au ja, da is was dran. Wie ist das mit den Forward-Bezeichnern, seit wann kann C das?
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

Affje

Treue Seele

  • »Affje« ist der Autor dieses Themas

Beiträge: 89

Beruf: Student

  • Private Nachricht senden

18

19.03.2011, 13:45

Danke Kobold, manche Fehler nerven mich regelrecht, so blöd und einfach zu lösen sind sie :(
Habe jetzt die Reihenfolge geändert und es klappt, danke euch.

*schäm*

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Affje« (19.03.2011, 13:53)


drakon

Supermoderator

Beiträge: 6 513

Wohnort: Schweiz

Beruf: Entrepreneur

  • Private Nachricht senden

19

19.03.2011, 13:55

Du hast da noch viele andere komische Sachen drin..

- Was zur Hölle soll z.B Zeile 5?!
- Die Rückgabe von new_object musst du auf den passenden Typen casten
- Forward Bezeichner, wie es BlueCobold angesprochen hat beachten (weiss nicht ob das C kann, C++ kanns auf jeden Fall nicht)
- In der main sollten a und b jeweils Zeiger auf Account sein.

Das hier kompiliert bei mir (VC 2010), was aber nicht heisst, dass es keine Fehler mehr drin haben kann

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h> /* malloc, exit, NULL */
#include <stdio.h>  /* printf */

typedef const char* String;
//typedef struct Account* Account; WTF?!

long next_number = 1;   /* nächste zu vergebende Kontonummer */

/* Konto. */
struct Account {
  long number;      /* Kontonummer. */
  String holder;    /* Kontoinhaber. */
  long balance;     /* Kontostand in Cent. */
};

/* Gebührenpflichtiges Konto */
struct ChargedAccount {
    struct Account Account;  /* Property-Vererbung struct Account*/
    int count;
};

/* Dynamisches Objekt der Größe n Byte erzeugen. */
void* new_object (int n) {
  void* t = malloc(n);
  if (t == NULL) {
    printf("new_object: out of memory\n");
    exit(1);
  }
  return t;
}

/* Betrag amount auf Konto this einzahlen. */
void deposit (struct Account* t, long amount) {
  t->balance += amount;
}

/* Betrag amount von Konto this abheben. */
void withdraw (struct Account* t, long amount) {
  t->balance -= amount;
}

/* Konto mit Inhaber h erzeugen. */
struct Account* new_account (String h) {
  struct Account* t = (Account*) new_object(sizeof(struct Account));
  t->number = next_number++;
  t->holder = h;
  t->balance = 0;
  return t;
}

/* gebührenpflichtiges Konto initialisieren */
void init_charged_Account (struct ChargedAccount* t ,String h){
    t->Account.number = next_number++;
    t->Account.holder = h;
    t->Account.balance = 0;
}

/* Neues gebührenpflichtiges Konto */
struct ChargedAccount* new_charged_account (String h){
    struct ChargedAccount* t = (ChargedAccount*) new_object(sizeof(struct ChargedAccount));
    init_charged_Account(t, h);
    return t;
}

/* Abhebung für gebührenpflichtige Konten */
void withdraw_ChargedAccount (struct ChargedAccount* t, long amount) {
    withdraw(&(t->Account), amount);
    t->count++;
}

/* Einzahlung für gebührenpflichtige Konten */
void deposit_ChargedAccount (struct ChargedAccount* t, long amount) {
    deposit(&(t->Account), amount);
    t->count++;
}

/* Nummer von Konto this liefern. */
long number (struct Account* t) {
  return t->number;
}

/* Inhaber von Konto this liefern. */
String holder (struct Account* t) {
  return t->holder;
}

/* Kontostand von Konto this liefern. */
long balance (struct Account* t) {
  return t->balance;
}



/* Betrag amount von Konto this auf Konto dest �berweisen. */
void transfer (struct Account* t, long amount,
                    struct Account* dest) {
  withdraw(t, amount);
  deposit(dest, amount);
}

/* Zeiger auf Kontoobjekt. */

int main () {
  /* Zwei Konten erzeugen. */
  Account* a = (Account*) new_account("Hans Maier");
  Account* b = (Account*) new_account("Fritz Müller");

  /* 1000 Cent auf Konto a einzahlen, */
  /* dann 300 Cent auf Konto b �berweisen. */
  deposit(a, 1000);
  transfer(a, 300, b);

  /* Kontodaten ausgeben. */
  printf("Konto a: %ld, %s, %ld\n",
   number(a), holder(a), balance(a));
  printf("Konto b: %ld, %s, %ld\n",
   number(b), holder(b), balance(b));

  return 0;
}

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

20

19.03.2011, 15:33

Zeile 5 fand ich auch sehr amüsant, so tiefgründig und up-to-date sind meine Infos aber dann auch wieder nicht, als dass ich mich zum Eimer machen wollte um nachzufragen, was Zeile 5 bedeuten soll.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

Werbeanzeige