template Queue - Compilerfehler
-
Hi,
hätte hier einen Mustercode, der leider nicht laufen will.
Quelle:
http://www.java2s.com/Tutorial/Cpp/0260__template/GenericQueue.htm#include<iostream> #include<iomanip> #include<ctype.h> using namespace std; template<class Type> class queue { private: Type size; Type start; Type end; Type *data; public: queue() { size=20; start=end=0; data=new Type[size]; } queue(Type n) { size=n; start=end=0; data=new Type[size]; } ~queue() { delete data; } void put(Type value) { if((start+1)size==end) { cout<<"\n***Queue is full!***\n"; return ; } data[start]=value; start=(start+1)size; cout<<"You have put a data into the queue!\n"; return; } Type get() { Type value; if(start==end) { cout<<"\n***Queue is empty!***\n"; return(0); } value=data[end]; end=(end+1)size; cout<<"\n Get "<<value<<" from the queue!\n"; return(value); } void clear() { start=end; cout<<"\n ***Queue is empty!***\n"; } void ShowQueue() { if(start==end) { cout<<"\n The queue has no data!\n"; return; } Type i; cout<<"\n The content of queue:\n"; for(i=end;i!=start;i=((i+1)size)) cout<<setw(5)<<data[i]; cout<<"\n\n"; } }; int main() { queue<char> ss(5); char value = 'a'; ss.put(value); ss.put('b'); value=ss.get(); ss.clear(); ss.ShowQueue(); system("pause"); return 0; }Fehlermeldung:
1>c:\dokumente und einstellungen\tschai\desktop\asd\asd\main.cpp(36) : error C2059: syntax error : ')' 1>c:\dokumente und einstellungen\tschai\desktop\asd\asd\main.cpp(37) : error C2143: syntax error : missing ';' before '{' 1>c:\dokumente und einstellungen\tschai\desktop\asd\asd\main.cpp(37) : warning C4553: '==' : operator has no effect; did you intend '='? 1>c:\dokumente und einstellungen\tschai\desktop\asd\asd\main.cpp(42) : error C2146: syntax error : missing ';' before identifier 'size'vielleicht kann mir ja jemand helfen, bin hier am verzweifeln!!

-
also wenn du das nicht rausfinden kannst...
-
pampy schrieb:
#include<iostream> #include<iomanip> #include<ctype.h> //1. heißt der header <ctype> und 2. wozu brauchst du ihn? using namespace std; template<class Type> class queue { private: Type size; //wieso vom template-typ? vll std::size_t bzw nen size_type bauen!? Type start; //wieso vom template-typ? Type end; //zumindest ein pointer sollte es schon sein^^ Type *data; public: queue() { //initialisierungsliste size=20; start=end=0; data=new Type[size]; } queue(Type n) { //schon wieder das problem type != size_type size=n; start=end=0; data=new Type[size]; } //übrigens: fällt dir was an den beiden konstruktoren auf? sehen sie evtl leicht ähnlich aus? ~queue() { delete data; //delete []data; wäre richtig } //hier hör ich auf, fehler zu suchen : P void put(Type value) { if((start+1)size==end) { cout<<"\n***Queue is full!***\n"; return ; } data[start]=value; start=(start+1)size; cout<<"You have put a data into the queue!\n"; return; } Type get() { Type value; if(start==end) { cout<<"\n***Queue is empty!***\n"; return(0); } value=data[end]; end=(end+1)size; cout<<"\n Get "<<value<<" from the queue!\n"; return(value); } void clear() { start=end; cout<<"\n ***Queue is empty!***\n"; } void ShowQueue() { if(start==end) { cout<<"\n The queue has no data!\n"; return; } Type i; cout<<"\n The content of queue:\n"; for(i=end;i!=start;i=((i+1)size)) cout<<setw(5)<<data[i]; cout<<"\n\n"; } }; int main() { queue<char> ss(5); char value = 'a'; ss.put(value); ss.put('b'); value=ss.get(); ss.clear(); ss.ShowQueue(); system("pause"); return 0; }was spricht gegen die standard lib? kennst du sie nur nicht gut genug?
http://www.cplusplus.com/reference/
ist ne tolle dokumentation ;o)bb
PS: Allein schon die ersten 3 Zeilen des Mustercodes sind so grausig, dass du ihn niemals nutzen solltest : D
-
Mein Rat: Such dir ein besseres Tutorial. Oder besser noch, kauf dir ein vernünftiges Buch wie z. B. den C++-Primer. Denn der Code dort ist ja furchtbar.

-
alles klar!
löscht den kackcode hier.
danke!