Schrift
[thread]1107[/thread]

Probleme mit struct



<< >> 10 Einträge, 1 Seite
renee
 2004-04-23 16:44
#10936 #10936
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Hi!

Ich habe in einem Programm folgendes struct:
Code: (dl )
1
2
3
4
5
6
7
8
9
/*
* Struct für die einzelnen Elemente
*/
struct TelBuchEintrag{
char *name;
char *telNummer;

TelBuchEintrag *nextEintrag;
};


und später den Code:
Code: (dl )
1
2
3
4
5
6
7
8
9
/*
* Neuen Telefonbucheintrag hinzufuegen
*/
bool addEntry(char* text, char* nummer){
TelBuchEintrag *neuerEintrag = new(TelBuchEintrag);
neuerEintrag->name = text;
neuerEintrag->telNummer = nummer;
neuerEintrag->nextEintrag = NULL;
}


Und beim kompilieren bekomme ich folgende Fehler:
Quote
(reneeb) Fri Apr 23 14:37:01 [-bash]
~/Tests/cpp 11> g++ Telefonliste.cpp
Telefonliste.cpp: In function `bool addEntry(char*, char*)':
Telefonliste.cpp:49: `letzerEintrag' undeclared (first use this function)
Telefonliste.cpp:49: (Each undeclared identifier is reported only once for each
function it appears in.)
Telefonliste.cpp:56: `struct TelBuchEintrag' has no member named `next'
Telefonliste.cpp: In function `char* getNummer()':
Telefonliste.cpp:86: `struct TelBuchEintrag' has no member named `nummer'


Was muss ich ändern??

(Ich merke schon, dass ich mich mal wieder mehr mit C++ beschäftigen muss...)
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
betterworld
 2004-04-23 17:23
#10937 #10937
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
Bist Du sicher, dass Du nicht aus Versehen ein anderes Programm kompiliert hast? Im Compiler-Output steht etwas von `letzterEintrag', aber dieser Bezeichner taucht im Source gar nicht auf!
esskar
 2004-04-23 17:28
#10938 #10938
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
Code: (dl )
1
2
3
4
5
6
struct TelBuchEintrag {
char *name;
char *telNummer;

struct TelBuchEintrag *nextEintrag;
};


besser

Code: (dl )
1
2
3
4
5
6
typedef struct tagTelBuchEintrag {
char *name;
char *telNummer;

struct tagTelBuchEintrag *nextEintrag;
} TelBuchEintrag, *PTelBuchEintrag;
renee
 2004-04-23 17:44
#10939 #10939
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
[quote=betterworld,23.04.2004, 15:23]Bist Du sicher, dass Du nicht aus Versehen ein anderes Programm kompiliert hast? Im Compiler-Output steht etwas von `letzterEintrag', aber dieser Bezeichner taucht im Source gar nicht auf![/quote]
letzterEintrag ist eine globale Variable, in der ein Pointer auf den zuletzt erstellten Eintrag zeigt...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
renee
 2004-04-23 17:49
#10940 #10940
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
@esskar: Kannst Du mir vielleicht erklären, was der Unterschied ist?


Hier mein bisheriger Code:
Code: (dl )
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
/*
* Programm, das eine Telefonliste erstellt. Hier koennen dynamisch neue Elemente
* hinzugefuegt werden.
* Autor: Renee Baecker
* Version: 0.1
* Datum: 23.04.2004
*/

#include <iostream>
#include <assert.h>

using namespace std;

/*
* Struct für die einzelnen Elemente
*/
typedef struct tagTelBuchEintrag{
char *name;
char *telNummer;

struct tagTelBuchEintrag *nextEintrag;
}TelBuchEintrag, *PTelBuchEintrag;

/*
* Globale Variablen zur Navigation innerhalb des Telefonbuchs
*/
TelBuchEintrag *ersterEintrag; //Zeiger auf den ersten Telefonbucheintrag
TelBuchEintrag *letzterEintrag; //Zeiger auf den letzten Telefonbucheintrag
long anzahlEintraege; //Anzahl der Eintraege insgesamt

/*
* Funktion zur Initialisierung des Telefonbuchs
*/
void initTelBuch(){
ersterEintrag = NULL;
letzterEintrag = NULL;
anzahlEintraege = 0;
}

/*
* Neuen Telefonbucheintrag hinzufuegen
*/
bool addEntry(char* text, char* nummer){
TelBuchEintrag *neuerEintrag = new(TelBuchEintrag);
neuerEintrag->name = text;
neuerEintrag->telNummer = nummer;
neuerEintrag->nextEintrag = NULL;

if(!letzerEintrag){ // wenn noch keine Eintraege vorhanden sind
ersterEintrag = neuerEintrag;
letzterEintrag = neuerEintrag;
anzahlEintraege++;
}
else{
TelBuchEintrag *tmpEintrag = letzterEintrag;
tmpEintrag->next = neuerEintrag;
letzterEintrag = neuerEintrag;
anzahlEintraege++;
}
return true;
}

/*
* Funktion, die den Namen zurueckgibt
*/
char* getName(){
char *name;

assert(anzahlEintraege);

if(anzahlEintraege){
name = ersterEintrag->name;
}
return name;
}

/*
* Funktion, die die Nummer zurueckgibt
*/
char* getNummer(){
char *nummer;

assert(anzahlEintraege);

if(anzahlEintraege){
nummer = ersterEintrag->nummer;
}
return nummer;
}

/*
* Funktion, die das erste Listenelement loescht.
*/
void delListElement(){
TelBuchEintrag *alterEintrag;

assert(anzahlEintraege);

if(anzahlEintraege){
alterEintrag = ersterEintrag;
ersterEintrag = ersterEintrag->nextEintrag;
delete alterEintrag;
}
}

/*
* Hauptprogramm
*/
int main(){
initTelBuch();
addEntry("Renee","0180/5632893");
cout<<getName()<<" - "<<getNummer()<<endl;
return 0;
}


Und der bringt diese Fehlermeldung:
Quote
(reneeb) Fri Apr 23 14:46:07 [-bash]
~/Tests/cpp 12> g++ Telefonliste.cpp
Telefonliste.cpp: In function `bool addEntry(char*, char*)':
Telefonliste.cpp:49: `letzerEintrag' undeclared (first use this function)
Telefonliste.cpp:49: (Each undeclared identifier is reported only once for each
function it appears in.)
Telefonliste.cpp:56: `struct tagTelBuchEintrag' has no member named `next'
Telefonliste.cpp: In function `char* getNummer()':
Telefonliste.cpp:86: `struct tagTelBuchEintrag' has no member named `nummer'
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
esskar
 2004-04-23 18:08
#10941 #10941
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
Code: (dl )
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
/*
* Programm, das eine Telefonliste erstellt. Hier koennen dynamisch neue Elemente
* hinzugefuegt werden.
* Autor: Renee Baecker
* Version: 0.1
* Datum: 23.04.2004
*/

#include <iostream>
#include <assert.h>

using namespace std;

/*
* Struct für die einzelnen Elemente
*/
typedef struct tagTelBuchEintrag{
char *name;
char *telNummer;

struct tagTelBuchEintrag *nextEintrag;
}TelBuchEintrag, *PTelBuchEintrag;

/*
* Globale Variablen zur Navigation innerhalb des Telefonbuchs
*/
TelBuchEintrag *ersterEintrag; //Zeiger auf den ersten Telefonbucheintrag
TelBuchEintrag *letzterEintrag; //Zeiger auf den letzten Telefonbucheintrag
long anzahlEintraege; //Anzahl der Eintraege insgesamt

/*
* Funktion zur Initialisierung des Telefonbuchs
*/
void initTelBuch(){
ersterEintrag = NULL;
letzterEintrag = NULL;
anzahlEintraege = 0;
}

/*
* Neuen Telefonbucheintrag hinzufuegen
*/
bool addEntry(char* text, char* nummer){
TelBuchEintrag *neuerEintrag = new(TelBuchEintrag);
neuerEintrag->name = text;
neuerEintrag->telNummer = nummer;
neuerEintrag->nextEintrag = NULL;

if(!letzterEintrag){ // wenn noch keine Eintraege vorhanden sind
ersterEintrag = neuerEintrag;
letzterEintrag = neuerEintrag;
anzahlEintraege++;
}
else{
TelBuchEintrag *tmpEintrag = letzterEintrag;
tmpEintrag->nextEintrag = neuerEintrag;
letzterEintrag = neuerEintrag;
anzahlEintraege++;
}
return true;
}

/*
* Funktion, die den Namen zurueckgibt
*/
char* getName(){
char *name = NULL;

assert(anzahlEintraege);

if(anzahlEintraege){
name = ersterEintrag->name;
}
return name;
}

/*
* Funktion, die die Nummer zurueckgibt
*/
char* getNummer(){
char *nummer = NULL;

assert(anzahlEintraege);

if(anzahlEintraege){
nummer = ersterEintrag->telNummer;
}
return nummer;
}

/*
* Funktion, die das erste Listenelement loescht.
*/
void delListElement(){
TelBuchEintrag *alterEintrag;

assert(anzahlEintraege);

if(anzahlEintraege){
alterEintrag = ersterEintrag;
ersterEintrag = ersterEintrag->nextEintrag;
delete alterEintrag;
}
}

/*
* Hauptprogramm
*/
int main(){
initTelBuch();
addEntry("Renee","0180/5632893");
cout<<getName()<<" - "<<getNummer()<<endl;
return 0;
}


so sollte es gehen...
PS: würde nicht mit globalen variablen arbeiten!
renee
 2004-04-23 18:17
#10942 #10942
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Danke!

Ich sollte wohl besser noch mal im Kindergarten anfangen. Wer lesen kann ist klar im Vorteil. Stand ja eigentlich alles in den Fehlermeldungen...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
esskar
 2004-04-23 18:24
#10943 #10943
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
jep!
SirLant
 2004-04-26 22:13
#10944 #10944
User since
2003-08-04
516 Artikel
BenutzerIn
[default_avatar]
Esskar weshalb die Typdefs? Es handelt sich hier doch um C++ und kein C und
ist somit doch nicht nötig.
--Programming today is a race between Software Enginers striving to build bigger and better idiot-proof Programs,
and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning!
esskar
 2004-04-26 23:06
#10945 #10945
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
[quote=SirLant,26.04.2004, 20:13]Esskar weshalb die Typdefs? Es handelt sich hier doch um C++ und kein C und
ist somit doch nicht nötig.[/quote]
das stimmt...
aber wenn man sich mal was angeöhnt hat! :)

außerdem hab ich somit die Möglichkeit, meine Funktionen in .dll und .h auszulagern, damit sie in C importiert werden können!
<< >> 10 Einträge, 1 Seite



View all threads created 2004-04-23 16:44.