Solved

template woes

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
is twain_32.dll cmpatible with windows 10 ? 10 237
VS2015 Redefinition errors 4 91
designing in object programming 12 107
Why  my code (program) build with old compiler? 11 113
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 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