Thread: Stack data structure using Templates

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Stack data structure using Templates

    I swear lately, i'm not getting anything right..and it is p*s**ng me off...this is what happens when I leave C++ to do something else...argh
    Code:
    template<typename T> 
    class MyOwnStack
    {
        public:
             void push(T x);
             T pop() ;
             MyOwnStack(int x) ;
        private:
        	  T *a ;
        	  int index;
    	      	  
    };
    template<typename T> MyOwnStack<T>::MyOwnStack(int x)
    {
       a = new T[x];
    }
    template<typename T> MyOwnStack::push(T x)
    {
       a[index++] = x ;	        
    }
    
    template<typename T> T MyOwnStack<T>::pop()
    {
        int a = a[index--];
    
        return a;
    }
    
    int main()
    {
     return 0;
    }
    Dev C++ doesn't compile this code, Vis Studio doesn't as well..i really don't know wtf is going wrong..can anyone enlighten me? Thanks
    Last edited by Eman; 05-09-2011 at 10:26 AM.
    You ended that sentence with a preposition...Bastard!

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    aargh..fixed the code, will upload incorrect code in a mo
    You ended that sentence with a preposition...Bastard!

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    template<typename T> void MyOwnStack<T>::push(T x)
    template<typename T> T MyOwnStack<T>::pop()
    {
    	int b = a[index--];
    	return b;
    }
    Now, I will give some advice.
    Visual C++ only parses template functions for syntax errors unless instantiated. So always do that. You will catch more errors then.
    Don't use the same variable names locally as you have on class level. If possible, prefix class level members with something, such as m_.
    Don't forget to delete your memory! Or you can use std::vector to dynamically grow it.
    Probably should have a default constructor and push should take arguments by const T&.

    Also, in the future, put the compile errors in your post.
    Last edited by Elysia; 05-09-2011 at 10:35 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    okie, i have this exact code..in Vis Studio
    Code:
    #include <iostreaM>
    using namespace std;
    template<typename Z>
    struct Node 
    {
        Z data ;
        Node *next ; 
        Node(Z data, Node *next)
        {
      	 this->data = data ;
      	 this->next = next ;
        }  	  
    };
    template<typename T> 
    class MyOwnStack
    {
        public:
             void push(T x);
             T pop() ;
             MyOwnStack<T>();
        private:
        	  Node<T> *stackPtr ;
        	  int index;
    	      	  
    };
    template<typename T> MyOwnStack<T>::MyOwnStack<T>()
    {
     	        stackPtr = NULL ;
     	       	        }
    template<typename T> void MyOwnStack<T>::push(T x)
    {
             Node<T> *t = stackPtr; 
    
             stackPtr = new Node<T>(x, t) ;
    }
    
    template<typename T> T MyOwnStack<T>::pop()
    {
       	T t = stackPtr->data ;
    	Node<T> *d = stackPtr ;
    	stackPtr = stackPtr->next;
    	delete d; 
    	return t; 
    }
    
    int main()
    {
        MyOwnStack<int> myStack ; 
        
        myStack.push(10) ;
        return 0;
    }
    but the problem is when i want to use .push(T x) it gives me ridiculous message

    Code:
    Error	1	error LNK2019: unresolved external symbol "public: void __thiscall MyStack<int>::push(int)" (?push@?$MyStack@H@@QAEXH@Z) referenced in function _main	c:\Users\Eman\documents\visual studio 2010\Projects\TemplatedDataStructures\TemplatedDataStructures\main.obj	TemplatedDataStructures
    what on earth is happening?
    Last edited by Eman; 05-09-2011 at 10:50 AM. Reason: It works in Dev C :(
    You ended that sentence with a preposition...Bastard!

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    Code:
    template<typename T> void MyOwnStack<T>::push(T x)
    template<typename T> T MyOwnStack<T>::pop()
    {
    	int b = a[index--];
    	return b;
    }
    Now, I will give some advice.
    Visual C++ only parses template functions for syntax errors unless instantiated. So always do that. You will catch more errors then.
    Don't use the same variable names locally as you have on class level. If possible, prefix class level members with something, such as m_.
    Don't forget to delete your memory! Or you can use std::vector to dynamically grow it.
    Probably should have a default constructor and push should take arguments by const T&.

    Also, in the future, put the compile errors in your post.
    yo, yeah I saw the error right after I posted...but the freaking error is still happening anyways
    You ended that sentence with a preposition...Bastard!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You didn't split these into their own files, did you? Ie, one header and one .cpp file for the stack?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I did..but in dev c++ they're in one file..Vis Studio has been giving me link errors lately..but it works most of the time. I doubt that's it though
    You ended that sentence with a preposition...Bastard!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot split a template declaration and its implementation. Put them both in the same file (header).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you can't be FREAKING serious! lol i lost potential hours of study on this crap..dang!
    so where ever I use type T...it must be in the same header...one like coming up

    EDIT:
    bloody hell...copy and paste to header file..0 errors..wow thanks dude.
    You ended that sentence with a preposition...Bastard!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All your stack needs to be in the header. The code that uses the stack does not need to.
    Templates are tricky beings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    All your stack needs to be in the header. The code that uses the stack does not need to.
    Templates are tricky beings.
    yep, i just copied the cpp of MyStack into it and it works fine..wasted too much time on this lol
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Stack Question With Templates
    By Anonymous Freak in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2003, 12:18 PM
  4. Stack errors using templates
    By Nakeerb in forum C++ Programming
    Replies: 6
    Last Post: 11-29-2002, 03:16 PM
  5. templates, generic data types
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2002, 02:29 AM