![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 7
Rep Power: 0
![]() |
hi, i've been learning c++ since 2 weeks and i'm having terribly troubles with this.
the point is that i was asked to build a stack class using templates that uses a circular chained list internally and i should also use the "Nodo" class in the same file. i did it (in notepad, by the way) but the compiler found many errors that i dont know how to fix. i would appreciate greatly if anyone could help me before monday. this is my code: stack.h template <class T> struct Nodo { public: T info; Nodo <T> *sig; Nodo(void) : info(0), sig(0) {} Nodo(T dato, Nodo <T>*s) : info(dato), sig(s) {} }; template <class T> class Stack{ private: Nodo<T> *inicio; public: //constructor Stack(): inicio(NULL){} //destructor ~Stack():{while(!empty()) pop();} //verifier bool empty():{return inicio==NULL;} //peek T& top(){return inicio->sig->info;} //in void push(const T& dato):{ Nodo <T> *P=new Nodo <T> (dato, NULL); if(inicio!=NULL) P->sig=inicio->sig; else inicio = P; inicio->sig=P; } //out T pop(){ Nodo <T> *P=inicio->sig; if(inicio==P)inicio=NULL; else inicio->sig=P->sig; return P->info; } //copy constructor Stack <T> (Stack const& src){ //no segura si <T> debe ir o no... Nodo <T> *P; inicio=NULL; P=src.inicio->sig; while(P!=inicio){ push(P->info); P=P->sig; } Push(P->info); } //overwhelm Stack <T> operator= (const Stack& src){ if(this!=&src){ while(!empty()) pop(); if(src.inicio==NULL) inicio = NULL; else{ Nodo <T> *P; inicio=NULL; P=src.inicio->sig; while(P!=inicio){ push(P->info); P=P->sig; } push(P->info); return *this; } } } } i tried to check it with this but it does not work #include <iostream> #include "P.h" int main() { int Data; Stack L; for(int k=20; k > 0; k--) L.push(k); while ( !L.empty() ) { Data = L.pop(); //L.drop(); cout << Data << " "; } return 0; } in cpp: 5: extraneous 'int' ignored 5: cannot declare 'main' to be a templete 5: semicolon missing after declaration of 'class Stack<T>' 8:parse error before ';' in h: ~Stack: 18: only constructors take base initializers 18: no base initializers given following ';' empty: 20:only constructors take base initializers 20: no base initializers given following ';' push: 24: only constructors take base initializers 24: no base initializers given following ';' Last edited by vitroblue; Jun 25th, 2005 at 10:30 PM. Reason: problem solved!! |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0
![]() |
you are missing a semicolon after the declaration of 'class Stack<T>'
Why is it so hard to read the messages that the compiler generates!?!?
__________________
How to ask questions the smart way |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
It seems that posting is almost harder than programming in c++.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 7
Rep Power: 0
![]() |
hey, don't be mad at me. i speak spanish, not english.
i know it says a semicolon is missing but i don't know where exactly. also i don't think a semicolon is missing. that kind of errors appear also when something else is missing, but i don't know what it could be. did you read my code or just the errors? in line 5 there's no Stack <T> declaration. Nor in the .h, nor in the .cpp. that's why i'm asking for help. .h line 5: 'Nodo <T> *sig;' or 'public:' .cpp line 5: '{' |
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0
![]() |
__________________
How to ask questions the smart way |
|
|
|
|
|
#6 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 597
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Jun 2005
Posts: 7
Rep Power: 0
![]() |
Quote:
but the errors in the cpp are still there... i tried a few things ( i also made a little correction according to a web example i found) but they don't disappear so i don't think my cpp is wrong. i wrote down in paper the whole .h code again and followed all the method instructions looking for any logic error. i found none, apparently they work ok, so i don't know what to do next. this is the "new" cpp i made (lies, i just added a little thing, but the results are the same) #include <iostream> #include "P.h" // this is coz i copy-pasted my header in a new file before correcting it int main() { int Data; Stack<int> L; for(int k=20; k > 0; k--) L.push(k); while ( !L.empty() ) { Data = L.top(); L.drop(); cout << Data << " "; } return 0; } the errors are: 4: semicolon missing after declaration of 'Stack<T>' 5: extraneos 'int' ignored 5: cannot declare main to be a template 5: semicolon missing after declaration of 'class Stack<T>' 7: bailing out //it is confused by previous errros |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Please place your code in tags to improve the readability. The probability of getting more responses will go up. There's a nice "How to Post...." thread at the top of the C forum. It's a small favor to your potential respondents in return for answers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 597
Rep Power: 4
![]() |
also, repost p.h because the problems seems to be there.
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jun 2005
Posts: 7
Rep Power: 0
![]() |
THANKS TO EVERYONE!!!
THE WHOLE PROYECT IS ALREADY DONE, not just the stack class but also the program that was going to use it. it was finished at 6:30pm Mexico City, Monterrey, Tijuana time. the problems were little details as the semicolons and so. other people noticed them. it is just that i have work with c++ just for a few weeks (2 or so) and those details still skip my attention but i promise i'll work harder than i did with java (mainly coz there are still 2 weeks of works, exams and proyects T.T) bye!! see you all around atte: Vitroblue |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|