Solved

template woes

Posted on 2000-03-15
5
316 Views
Last Modified: 2010-04-10
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
0
Comment
Question by:BudVVeezer
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:loumf
ID: 2621133
declare temp as
L_Stack<char>::Node *temp = new L_Stack<char>::Node;

If that doesn't work, the error might be more explanatory.
0
 
LVL 1

Expert Comment

by:loumf
ID: 2621136
sorry I should have said:

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

oops

0
 
LVL 3

Author Comment

by:BudVVeezer
ID: 2621155
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;
}
0
 
LVL 1

Accepted Solution

by:
loumf earned 50 total points
ID: 2621296
I changed the nested class def to

struct Node
{
  Node *Prev, *Next;
  T data;
};

and it worked.  I think the typedef becomes a new type.
0
 
LVL 3

Author Comment

by:BudVVeezer
ID: 2621312
works just fine now...that is really odd.  I've NEVER had a problem with doing my nodes that way before..  Well, thanks!

~Aaron
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question