Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 11th, 2005, 1:23 PM   #1
vitroblue
Newbie
 
Join Date: Jun 2005
Posts: 7
Rep Power: 0 vitroblue is on a distinguished road
Question i desperately need help with this: templates in circular stacks [solved]

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!!
vitroblue is offline   Reply With Quote
Old Jun 11th, 2005, 1:29 PM   #2
jubitzu
Newbie
 
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0 jubitzu is on a distinguished road
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!?!?
jubitzu is offline   Reply With Quote
Old Jun 11th, 2005, 1:50 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Jun 11th, 2005, 2:10 PM   #4
vitroblue
Newbie
 
Join Date: Jun 2005
Posts: 7
Rep Power: 0 vitroblue is on a distinguished road
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: '{'
vitroblue is offline   Reply With Quote
Old Jun 11th, 2005, 2:18 PM   #5
jubitzu
Newbie
 
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0 jubitzu is on a distinguished road
Lea esto
jubitzu is offline   Reply With Quote
Old Jun 11th, 2005, 2:38 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 597
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
void push(const T& dato):{
Remove the semicolon from the above line!
Ancient Dragon is offline   Reply With Quote
Old Jun 11th, 2005, 3:28 PM   #7
vitroblue
Newbie
 
Join Date: Jun 2005
Posts: 7
Rep Power: 0 vitroblue is on a distinguished road
Quote:
Originally Posted by Ancient Dragon
Remove the semicolon from the above line!
thank you, ancient dragon! i tried what you said but it only moved the error upward a line. then i nothiced i had another semicolon misplaced so i erased it also and now there are no errors in the header. thank you a lot!!
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
vitroblue is offline   Reply With Quote
Old Jun 11th, 2005, 5:54 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 11th, 2005, 8:42 PM   #9
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 597
Rep Power: 4 Ancient Dragon is on a distinguished road
also, repost p.h because the problems seems to be there.
Ancient Dragon is offline   Reply With Quote
Old Jun 11th, 2005, 9:07 PM   #10
vitroblue
Newbie
 
Join Date: Jun 2005
Posts: 7
Rep Power: 0 vitroblue is on a distinguished road
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
vitroblue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC