Computer Science Canada Implementing a template class Stack |
Author: | simonsayz [ Sat Apr 11, 2009 1:17 pm ] | ||
Post subject: | Implementing a template class Stack | ||
I'm using Visual C++ 2008 Express Edition. I'm getting the same three compile errors each time I compile this project. I don't know if my syntax for the template class is correct or not but I cannot figure it out. Any help or tips would be appreciated. The three errors are: stack.h(10) : error C2143: syntax error : missing ';' before '<' stack.h(22) : see reference to class template instantiation 'Stack<ItemType>' being compiled (Warning) stack.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int stack.h(10) : error C2238: unexpected token(s) preceding ';' Now I know I will have A LOT of compile errors once these three errors are gone so don't worry about my function definitions. I haven't even had a chance to check if they are right or not. Also the errors all seem to point to: NodeType<ItemType>* topPtr, for my private variable and the last "};" of my class.
|
Author: | wtd [ Sat Apr 11, 2009 2:36 pm ] |
Post subject: | RE:Implementing a template class Stack |
While I don't think I have the energy for a deep analysis, try moving the declaration for the NodeType struct to above the declaration for the Stack class. Stack shouldn't know what NodeType is until NodeType is declared. |
Author: | simonsayz [ Sat Apr 11, 2009 3:17 pm ] |
Post subject: | RE:Implementing a template class Stack |
I tried that already and didn't work. I'm almost sure it has something to do with the template<class ItemType>. Thanks for the tip though. Any other quick tips would help. |
Author: | bbi5291 [ Sat Apr 11, 2009 3:52 pm ] | ||||||||
Post subject: | Re: Implementing a template class Stack | ||||||||
I no longer have Visual Studio 2008, but I tried to compile your code on g++ and I conclude: 1. Yes, you have to put the NodeType declaration before the Stack declaration. 2. Whenever you use the name "NodeType" outside of the NodeType declaration itself, you have to include template arguments. For example,
not just
The compiler cannot be expected to guess that you want to use the template argument of some other class as the template argument to NodeType. 3. You can't name your exception "exception" (as in "catch(bad_alloc exception)") because exception is a type that has already been defined (in fact, bad_alloc inherits from it, I believe.) 4. EmptyStack and FullStack aren't declared... simply including the lines
should fix this. Also, I don't think it's strictly necessary but you should specify that the functions Pop and Push throw the exceptions that they do (I'm not sure if this is for readability reasons, or what):
|
Author: | simonsayz [ Sat Apr 11, 2009 4:29 pm ] |
Post subject: | RE:Implementing a template class Stack |
ah ha! Thanks bbi5291, I did include <ItemType> each time I declared NodeType* and also used a different method instead of using exceptions and those compile errors FINALLY disappeared. Thanks a ton. Will update my code when finished and error free. |
Author: | simonsayz [ Sat Apr 11, 2009 5:28 pm ] | ||||
Post subject: | RE:Implementing a template class Stack | ||||
Alright I finally finished without any compile errors. Thanks bbi5291 and wtd for helping me out.
Mod Edit: Fixed broken syntax tags.
|