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

New Topic/Question
Reply




MultiQuote




|