Solved

template woes

Posted on 2000-03-15
5
308 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
Comment Utility
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
Comment Utility
sorry I should have said:

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

oops

0
 
LVL 3

Author Comment

by:BudVVeezer
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 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

11 Experts available now in Live!

Get 1:1 Help Now