Template Stack Class

Can I create a vector of an undetermined type?

Page 1 of 1

12 Replies - 1320 Views - Last Post: 27 December 2009 - 09:24 PM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Template Stack Class

Post icon  Posted 26 December 2009 - 09:04 PM

Well ever since I read a few good tutorials by our friend Born2Code:
http://www.dreaminco...wtopic10157.htm
http://www.dreaminco...wtopic37428.htm

I decided to try to first design a templated stack class based on a vector container.
It seems that C++ doesn't like me declaring a vector of a data type that is yet to be defined.
Is there a way around this? I really do not want to use arrays, they're so.... static. I know I can expand them but I hate using new and delete.

Anyway, this class is not complete (still needs a constructor) but I performed a compile to check out the syntax and all.
Naturaly, nothing can possible go right in a project so i get this:

Compiler Output: (GCC-C++ under C::B )
F:\Development\UniStack\UniStack.h:11: error: ISO C++ forbids declaration of `vector' with no type
F:\Development\UniStack\UniStack.h:11: error: expected `;' before '<' token



For these source files:

UniStack.h:
/// Written by James Thigpen
/// Dec 26th 09
#ifndef UNISTACK_H_INCLUDED
#define UNISTACK_H_INCLUDED

            template <class D_Type> // Where as T is type
            class UniStack
            {
               private:

                vector<D_Type> Data; // body of stack.
                unsigned int   Top;  // top of stack. The value assigned here means that the stack is un-initialized.

               public:

               // Constructor
               UniStack(void);

            //  Return          Name           Arguments
                void            push          (D_Type Token);
                D_Type          pop           (void);
                unsigned int    top           (void);
                void            empty         (void);               // Empties vector stack container
                unsigned int    length        (void);               // Returns stack length
                D_Type          peek          (unsigned int layer); // Returns a layers (element of stack) data without it being on top or poped.


            };
                const int       stack_empty   = 1;
                const int       exceed_stack  = 2;
#endif // UNISTACK_H_INCLUDED



UniStack.cpp:
/// Written by James Thigpen
/// Dec 26th 09

#include "UniStack.h"

void UniStack::push(D_Type Token)
{
     UniStack::Top++;
      UniStack::Data.push_back(Token);
}

D_Type UniStack::pop(void)
{
    if(UniStack::Data.size() < 0)
    {
       throw UniStack::stack_empty; // The stack is empty, NO ELEMENTS!
    }
    D_Type TmpData = UniStack::Data.at(UniStack::Top); // Get a copy of the current top.
    UniStack::Top--; // decrement Top.
    UniStack::Data.pop_back(); // pop the top element of the vector container for the stack
    return TmpData;
}

unsigned int UniStack::top(void)
{
    return Top;
}

void UniStack::empty(void)
{
    UniStack::Top = -1;
    UniStack::Data.clear();
}

unsigned int UniStack::length(void)
{
    return UniStack::Data.size();
}

D_Type UniStack::peek(unsigned int layer)
{
    if(layer > UniStack::Data.size())
    {
        throw UniStack::exceed_stack; // Layer is outside stack (ie, that element does not exist!)
    }
        return UniStack::Data.at(layer);
}





ExampleMain.cpp:
/// Written by James Thigpen
/// Dec 26th 09

/// EXAMPLE OF USE OF UniStack

#include "UniStack.h"
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
    UniStack<char> MyCharStack;
    MyCharStack.push('h');
    MyCharStack.push('e');
    MyCharStack.push('l');
    MyCharStack.push('l');
    MyCharStack.push('o');
    std::cout<<"Current Stack (peek): " << std::endl;
          for(unsigned int a = 0; a < MyCharStack.length();a++)
          {
              std::cout << "Element: " << a << ": " << MyCharStack.peek(a) << std::endl;
          }
 return 1;
}




I would really like to avoid the use of arrays.
Any thoughts?

Thanks all.

This post has been edited by Delta_Echo: 26 December 2009 - 09:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Template Stack Class

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • Posts: 3,982
  • Joined: 09-June 09

Re: Template Stack Class

Posted 26 December 2009 - 09:25 PM

there should be a problem with using a template with a vector. What errors are you getting?
Was This Post Helpful? 1
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Template Stack Class

Posted 26 December 2009 - 09:30 PM

My compiler errors are listed in the original post.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • Posts: 3,982
  • Joined: 09-June 09

Re: Template Stack Class

Posted 26 December 2009 - 09:43 PM

well your not declaring your template classes before defining your functions


such as
// whats D_Type?
//you have to set your template for each function
void UniStack::push(D_Type Token)
{	
 UniStack::Top++;	
  UniStack::Data.push_back(Token);
}


This post has been edited by ImaSexy: 26 December 2009 - 09:43 PM

Was This Post Helpful? 1
  • +
  • -

#5 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Template Stack Class

Posted 26 December 2009 - 10:01 PM

I thought i just had to set the class as a template and everything else would would follow. Im going to check some tutorials. Btw, so can i use a vector As i do in the template?
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • Posts: 3,982
  • Joined: 09-June 09

Re: Template Stack Class

Posted 26 December 2009 - 10:07 PM

No you have to redefine the template as far as I know if you going to define the functions ouside the class. But yeah your vector is fine
Was This Post Helpful? 1
  • +
  • -

#7 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,032
  • Joined: 14-September 07

Re: Template Stack Class

Posted 26 December 2009 - 10:14 PM

Putting template implementation in a separate file from the definition will end up giving you linker errors. Put it in the same file [header] after the class definition.
Was This Post Helpful? 1
  • +
  • -

#8 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Template Stack Class

Posted 26 December 2009 - 10:28 PM

Ok,I will try in the morning, going to sleep. Thanks both yall :)
Was This Post Helpful? 0
  • +
  • -

#9 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Template Stack Class

Posted 27 December 2009 - 03:06 AM

In addition to KYA's advice, remember that 'UniStack' is not a class, its a template (a blueprint or cookie cutter); so when defining the template members, you'll need to specify them as such. i.e.

template<typename D_Type>
void UniStack<D_Type>::push(D_Type Token) 
'UniStack' on its own is meaningless since no class called UniStack exists.
Was This Post Helpful? 1
  • +
  • -

#10 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Template Stack Class

Posted 27 December 2009 - 02:53 PM

Ah, thanks bench. I was operating under the ideas that:
1. Functions of a templated class would automaticaly take on the template.
2. A templeted class was just an extension of a class.
Was This Post Helpful? 0
  • +
  • -

#11 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Template Stack Class

Posted 27 December 2009 - 03:36 PM

Ok, I am still getting an error with my vector declaration.

My header:
/// Written by James Thigpen
/// Dec 26th 09
#ifndef UNISTACK_H_INCLUDED
#define UNISTACK_H_INCLUDED

			namespace UniStackExceptions
			{
				 // Will store all variables used to id exceptions
				 const int	   stack_empty   = 1;
				 const int	   exceed_stack  = 2;
			}

			template <class D_Type> // Where as D_Type is type
			class UniStack
			{
			   private:

				vector<D_Type> Data; // body of stack.
				unsigned int   Top;  // top of stack. The value assigned here means that the stack is un-initialized.

			   public:

			   // Constructor
			   UniStack(void);

			//  Return		  Name		   Arguments
				void			push		  (D_Type Token);
				D_Type		  pop		   (void);
				unsigned int	top		   (void);
				void			empty		 (void);			   // Empties vector stack container
				unsigned int	length		(void);			   // Returns stack length
				D_Type		  peek		  (unsigned int layer); // Returns a layers (element of stack) data without it being on top or poped.


			};
				const int	   stack_empty   = 1;
				const int	   exceed_stack  = 2;

template <class D_Type>
void UniStack<D_Type>::push(D_Type Token)
{
	 UniStack<D_Type>::Top++;
	  UniStack<D_Type>::Data.push_back(Token);
}

template <class D_Type>
D_Type UniStack<D_Type>::pop(void)
{
	if(UniStack<D_Type>::Data.size() < 0)
	{
	   throw UniStackExceptions::stack_empty; // The stack is empty, NO ELEMENTS!
	}
	D_Type TmpData = UniStack<D_Type>::Data.at(UniStack<D_Type>::Top); // Get a copy of the current top.
	UniStack<D_Type>::Top--; // decrement Top.
	UniStack<D_Type>::Data.pop_back(); // pop the top element of the vector container for the stack
	return TmpData;
}

template <class D_Type>
unsigned int UniStack<D_Type>::top(void)
{
	return Top;
}

template <class D_Type>
void UniStack<D_Type>::empty(void)
{
	UniStack<D_Type>::Top = -1;
	UniStack<D_Type>::Data.clear();
}

template <class D_Type>
unsigned int UniStack<D_Type>::length(void)
{
	return UniStack<D_Type>::Data.size();
}

template <class D_Type>
D_Type UniStack<D_Type>::peek(unsigned int layer)
{
	if(layer > UniStack<D_Type>::Data.size())
	{
		throw UniStackExceptions::exceed_stack; // Layer is outside stack (ie, that element does not exist!)
	}
		return UniStack<D_Type>::Data.at(layer);
}
#endif // UNISTACK_H_INCLUDED




My main:
/// Written by James Thigpen
/// Dec 26th 09

/// EXAMPLE OF USE OF UniStack

#include "UniStack.h"
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
	UniStack<char> MyCharStack;
	MyCharStack.push('h');
	MyCharStack.push('e');
	MyCharStack.push('l');
	MyCharStack.push('l');
	MyCharStack.push('o');
	std::cout<<"Current Stack (peek): " << std::endl;
		  for(unsigned int a = 0; a < MyCharStack.length();a++)
		  {
			  std::cout << "Element: " << a << ": " << MyCharStack.peek(a) << std::endl;
		  }
 return 1;
}




Compiler:
F:\Development\UniStack\UniStack.h:18: error: ISO C++ forbids declaration of `vector' with no type
F:\Development\UniStack\UniStack.h:18: error: expected `;' before '<' token
F:\Development\UniStack\UniStack.h: In member function `void UniStack<D_Type>::push(D_Type) [with D_Type = char]':
F:\Development\UniStack\ExampleMain.cpp:16:   instantiated from here
F:\Development\UniStack\UniStack.h:42: error: `Data' is not a member of `UniStack<char>'
F:\Development\UniStack\UniStack.h: In member function `unsigned int UniStack<D_Type>::length() [with D_Type = char]':
F:\Development\UniStack\ExampleMain.cpp:22:   instantiated from here
F:\Development\UniStack\UniStack.h:74: error: `Data' is not a member of `UniStack<char>'
F:\Development\UniStack\UniStack.h: In member function `D_Type UniStack<D_Type>::peek(unsigned int) [with D_Type = char]':
F:\Development\UniStack\ExampleMain.cpp:24:   instantiated from here
F:\Development\UniStack\UniStack.h:80: error: `Data' is not a member of `UniStack<char>'
F:\Development\UniStack\UniStack.h:84: error: `Data' is not a member of `UniStack<char>'


Why wont it let me declare a vector?

This post has been edited by Delta_Echo: 27 December 2009 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#12 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Template Stack Class

Posted 27 December 2009 - 04:36 PM

I can't see that you've added #include <vector> anywhere in your code? (And you'll probably want to add std:: or a using declaration too)

Also, you haven't defined your constructor (Which is empty, so you may aswell delete it and use the compiler-generated one)

This post has been edited by Bench: 27 December 2009 - 04:37 PM

Was This Post Helpful? 1
  • +
  • -

#13 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Template Stack Class

Posted 27 December 2009 - 09:24 PM

Thanks, it is now compiling and operating as expected. I always forget that vector is inside the std namespace. You all get Thanks :)

Can i mark this subject as solved or does an admin do that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1