Link to home
Start Free TrialLog in
Avatar of BudVVeezer
BudVVeezer

asked on

template woes

here's the code:

template <class T>
void L_Stack<T>::append_node(T x)
{
      Node *temp = new Node;

      temp->Prev = last;      //set the prev
      temp->Next = NULL;      //set the next
      temp->data = x;            //set the data
      last->Next = temp;      //fix the last

      last = temp;            //make a new last
}

here's the error:
c:\my documents\programming\project 6\l_stack.h(78) : error C2440: '=' : cannot convert from 'L_Stack<char>::Node *' to 'struct Node *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Line 78 is:
temp->Prev = last;      //set the prev

What is going on with this??  I don't know much about templates...so!

~Aaron
Avatar of loumf
loumf

declare temp as
L_Stack<char>::Node *temp = new L_Stack<char>::Node;

If that doesn't work, the error might be more explanatory.
sorry I should have said:

L_Stack<T>::Node *temp = new L_Stack<T>::Node;

oops

Avatar of BudVVeezer

ASKER

I just switched it and I get the exact same error message(and I did try that once before)...that's what I find so darn odd.  Here's the entire listing of the file.

template <class T>
class L_Stack
{
      public:
            L_Stack(void);
            int empty(void);
            int depth(void);
            void push(const T);
            int pop(T &);
            int peek(T &);

      private:
            typedef struct
            {
                  struct Node *Prev, *Next;
                  T data;
            }Node;

            void append_node(T x);
            void remove_last_node(void);

            Node *last;      //the top of the l_stack
            int size;      //how many elements currently in the list
};

template <class T>
L_Stack<T>::L_Stack(void)
{
      size = 0;      //nothing in the list
      last = NULL;      //no nodes
}

template <class T>
int L_Stack<T>::depth(void)
{
      return size;      //the size of the stack
}

template <class T>
int L_Stack<T>::empty(void)
{
      return size == 0;      //if the size is 0, then the stack is empty
}

template <class T>
void L_Stack<T>::push(const T x)
{
      append_node(x);      //make the new node
      size++;      //increment the size of the list
}

template <class T>
int L_Stack<T>::peek(T &x)
{
      if(last)      //if there's a node there
            x = last->data;      //show the data, don't delete the node

      return 1;
}

template <class T>
int L_Stack<T>::pop(T &x)
{
      x = last->data;            //set the x
      remove_last_node();      //get rid of the node
      size--;      //decrement the size of the list

      return 1;
}

/***********PRIVATE FUNCTIONS*************/
//when we're adding a node in a stack, it ALWAYS goes on the *last
template <class T>
void L_Stack<T>::append_node(T x)
{
      //Node *temp = new Node;
      L_Stack<T>::Node *temp = new L_Stack<T>::Node;


      temp->Prev = last;      //set the prev
      temp->Next = NULL;      //set the next
      temp->data = x;            //set the data
      last->Next = temp;      //fix the last

      last = temp;            //make a new last
}

template <class T>
void L_Stack<T>::remove_last_node(void)
{
      Node *temp = last;      //temp var to be destroyed

      /*This is where the doubly linked list comes in handy!  You don't
      have to search through the list of nodes to find the PREVIOUS(Prev)
      node so we can do this operation.  This cuts down on seek time a lot,
      as well as cleans up the code*/

      //the previous node now points to NULL
      last->Prev->Next = NULL;
      //delete the last node
      delete temp;
}
ASKER CERTIFIED SOLUTION
Avatar of loumf
loumf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
works just fine now...that is really odd.  I've NEVER had a problem with doing my nodes that way before..  Well, thanks!

~Aaron