Solved

template woes

Posted on 2000-03-15
5
312 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now