23. april 2004 - 10:45
Der er
14 kommentarer og 1 løsning
til class
okay har fundet noget kildkode der virker men skal bruge det som en class isstedet for men efter det er lavet om til en class er volder følgendelinier problemer =( #define NIL &sentinel static NodeType sentinel = { &sentinel, &sentinel, 0, BLACK}; static NodeType *root = NIL; disse linier står inde i class navn{ bla bla bla } mener ikke man kan bruge define inde i klasser men er nogle der kan hjælpe med at skrive om i linier så de passer til class
Annonceindlæg fra HP
23. april 2004 - 10:48
#1
Du kan bruge #define hvor som helst. Den håndteres af pre processoren inden C elelr C++ scanner & parser kommer i funktion.
23. april 2004 - 11:33
#2
du kunne evt. bruge en const static const typenavnpåsentinel *NIL = &sentinel;
23. april 2004 - 11:48
#3
først og fremmest tak for de hurtige svar, i kan lige se fejrapporten her g++ -O -I/usr/local/projects/disk02/algorithm/LEDA-4.3/incl -c rbtree.cc rbtree.cc:27: `sentinel' was not declared in this scope rbtree.cc:27: `sentinel' was not declared in this scope rbtree.cc:27: invalid in-class initialization of static data member of non-integral type `rbtree::Node' rbtree.cc:30: `$sentinel' was not declared in this scope rbtree.cc:30: ISO C++ forbids in-class initialization of non-const static member `root' rbtree.cc:184: warning: all member functions in class `rbtree' are private rbtree.cc: In method `void rbtree::rotateLeft (rbtree::Node *)': rbtree.cc:43: `$sentinel' undeclared (first use this function) rbtree.cc:43: (Each undeclared identifier is reported only once for each function it appears in.) make: *** [rbtree.o] Error 1 har prøvet at kigge på jeres svar men har stdig problemer beklager =)
23. april 2004 - 11:53
#4
Linie 30 fejlen - ret: class X { ... static NodeType *root = NIL; ... } til: class X { ... static NodeType *root; ... } NodeType *X::root = NIL;
23. april 2004 - 11:55
#5
Med hensyn til linie 184 fejlen skal du nok have lavet nogle af metoderne public. class X { private: ... public: ... }
23. april 2004 - 11:56
#6
Med hensyn til linie 27 og 43 fejlene skal vi nok se lidt mere kode.
23. april 2004 - 11:57
#7
Jeg vil også lige ligge et svar
23. april 2004 - 12:40
#8
class rbtree{ typedef double KeyType; typedef enum { BLACK, RED } nodeColor; typedef struct Node { struct Node *left; /* left child */ struct Node *right; /* right child */ struct Node *parent; /* parent */ nodeColor color; /* node color (BLACK, RED) */ KeyType key; /* key used for searching */ } NodeType; //typedef NodeType *iterator; #define NIL &sentinel /* all leafs are sentinels */ static NodeType sentinel = { &sentinel, &sentinel, 0, BLACK}; static NodeType *root; /* root of Red-Black tree */ void rotateLeft(NodeType *x) { /************************** * rotate node x to left * **************************/ NodeType *y = x->right; /* establish x->right link */ x->right = y->left; if (y->left != NIL) y->left->parent = x; /* establish y->parent link */ if (y != NIL) y->parent = x->parent; if (x->parent) { if (x == x->parent->left) x->parent->left = y; else x->parent->right = y; } else { root = y; } /* link x and y */ y->left = x; if (x != NIL) x->parent = y; } void rotateRight(NodeType *x) { NodeType *y = x->left; /* establish x->left link */ x->left = y->right; if (y->right != NIL) y->right->parent = x; /* establish y->parent link */ if (y != NIL) y->parent = x->parent; if (x->parent) { if (x == x->parent->right) x->parent->right = y; else x->parent->left = y; } else { root = y; } /* link x and y */ y->right = x; if (x != NIL) x->parent = y; } void insertFixup(NodeType *x) { /* check Red-Black properties */ while (x != root && x->parent->color == RED) { /* we have a violation */ if (x->parent == x->parent->parent->left) { NodeType *y = x->parent->parent->right; if (y->color == RED) { /* uncle is RED */ x->parent->color = BLACK; y->color = BLACK; x->parent->parent->color = RED; x = x->parent->parent; } else { /* uncle is BLACK */ if (x == x->parent->right) { /* make x a left child */ x = x->parent; rotateLeft(x); } /* recolor and rotate */ x->parent->color = BLACK; x->parent->parent->color = RED; rotateRight(x->parent->parent); } } else { /* mirror image of above code */ NodeType *y = x->parent->parent->left; if (y->color == RED) { /* uncle is RED */ x->parent->color = BLACK; y->color = BLACK; x->parent->parent->color = RED; x = x->parent->parent; } else { /* uncle is BLACK */ if (x == x->parent->left) { x = x->parent; rotateRight(x); } x->parent->color = BLACK; x->parent->parent->color = RED; rotateLeft(x->parent->parent); } } } root->color = BLACK; } void insert(KeyType key) { NodeType* current = new NodeType; NodeType* parent = new NodeType; NodeType* x = new NodeType; /* find future parent */ current = root; parent = 0; while (current != NIL) { if(current->key == key) { cerr << "Den indsatte knude har en allerede benyttet nøgle" << endl; break; } parent = current; current = (key < current->key) ? current->left : current->right; } /* setup new node */ x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; x->key = key; /* insert node in tree */ if(parent) { if(key < parent->key) parent->left = x; else parent->right = x; } else { root = x; } insertFixup(x); } };//class end NodeType *X::root = NIL; Det skulle vist være det hele og endnu engang tak for SUPER hurtige svar =)
23. april 2004 - 12:49
#9
Den kan ikke lide den her: static NodeType sentinel = { &sentinel, &sentinel, 0, BLACK}; Hvor du forsøger at initialisere en struktur med adressen på strukturen selv.
23. april 2004 - 12:50
#10
NodeType *X::root = NIL; kræver at NodeType er defineret udenfor klassen.
23. april 2004 - 12:56
#11
Det er jeg klar over men er der nogle måde at initialisere en struktur med adressen på strukturen selv ?? du får point alligevel men håber du kan svare på det sidste spørgsmål
23. april 2004 - 13:07
#12
Prøv at ændre: static NodeType sentinel = { &sentinel, &sentinel, 0, BLACK}; til: static NodeType sentinel; inde i klassen og: NodeType rbtree::sentinel = { &sentinel, &sentinel, 0, BLACK}; udenfor klassen.
23. april 2004 - 13:07
#13
NodeType *X::root = NIL; skal også være: NodeType *rbtree::root = NIL;
26. april 2004 - 11:44
#14
okay har fået lavet det meste om nu så tak for det bare lige et sidste ting #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <iostream.h> class rbtree{ typedef double KeyType; typedef enum { BLACK, RED }nodeColor; typedef struct Node { struct Node *left; /* left child */ struct Node *right; /* right child */ struct Node *parent; /* parent */ nodeColor color; /* node color (BLACK, RED) */ KeyType key; /* key used for searching */ } NodeType; #define NIL &sentinel /* all leafs are sentinels */ static NodeType sentinel; static NodeType *root; /* root of Red-Black tree */ private: void rotateLeft(NodeType *x) { /************************** * rotate node x to left * **************************/ NodeType *y = x->right; /* establish x->right link */ x->right = y->left; if (y->left != NIL) y->left->parent = x; /* establish y->parent link */ if (y != NIL) y->parent = x->parent; if (x->parent) { if (x == x->parent->left) x->parent->left = y; else x->parent->right = y; } else { root = y; } /* link x and y */ y->left = x; if (x != NIL) x->parent = y; } void rotateRight(NodeType *x) { NodeType *y = x->left; /* establish x->left link */ x->left = y->right; if (y->right != NIL) y->right->parent = x; /* establish y->parent link */ if (y != NIL) y->parent = x->parent; if (x->parent) { if (x == x->parent->right) x->parent->right = y; else x->parent->left = y; } else { root = y; } /* link x and y */ y->right = x; if (x != NIL) x->parent = y; } void insertFixup(NodeType *x) { /* check Red-Black properties */ while (x != root && x->parent->color == RED) { /* we have a violation */ if (x->parent == x->parent->parent->left) { NodeType *y = x->parent->parent->right; if (y->color == RED) { /* uncle is RED */ x->parent->color = BLACK; y->color = BLACK; x->parent->parent->color = RED; x = x->parent->parent; } else { /* uncle is BLACK */ if (x == x->parent->right) { /* make x a left child */ x = x->parent; rotateLeft(x); } /* recolor and rotate */ x->parent->color = BLACK; x->parent->parent->color = RED; rotateRight(x->parent->parent); } } else { /* mirror image of above code */ NodeType *y = x->parent->parent->left; if (y->color == RED) { /* uncle is RED */ x->parent->color = BLACK; y->color = BLACK; x->parent->parent->color = RED; x = x->parent->parent; }else { /* uncle is BLACK */ if (x == x->parent->left) { x = x->parent; rotateRight(x); } x->parent->color = BLACK; x->parent->parent->color = RED; rotateLeft(x->parent->parent); } } } root->color = BLACK; } public: void insert(KeyType key) { NodeType* current = new NodeType; NodeType* parent = new NodeType; NodeType* x = new NodeType; /* find future parent */ current = root; parent = 0; while (current != NIL) { if(current->key == key) { cerr << "Den indsatte knude har en allerede benyttet nøgle" << endl; break; } parent = current; current = (key < current->key) ? current->left : current->right; } /* setup new node */ x->parent = parent; x->left = NIL; x->right = NIL; x->color = RED; x->key = key; /* insert node in tree */ if(parent) { if(key < parent->key) parent->left = x; else parent->right = x; } else { root = x; } insertFixup(x); } }; NodeType rbtree::sentinel = { &sentinel, &sentinel, 0, BLACK}; NodeType rbtree::root = NIL; compiles dette fås følgende fejl Norbtree.cc:168: syntax error before `::' rbtree.cc:170: syntax error before `::' make: *** [rbtree.o] Error 1 deType rbtree::sentinel = { &sentinel, &sentinel, 0, BLACK
26. april 2004 - 12:03
#15
Prøv og flyt den her: typedef struct Node { struct Node *left; /* left child */ struct Node *right; /* right child */ struct Node *parent; /* parent */ nodeColor color; /* node color (BLACK, RED) */ KeyType key; /* key used for searching */ } NodeType; udenfor class definitionen.
Kurser inden for grundlæggende programmering