Help... Template Question for my class

Posted on 2001-08-10
Last Modified: 2010-04-02
When I want to compile the following code, I got a bunch of errors that I don't understand... please help.

#include "llist.h"
#include <iostream.h>

int main ()
  LinkedList<int> *test;
  test = new LinkedList<int> (1);
  return 0;

#ifndef _LLIST_H_
#define _LLIST_H_

template <class T>
class ListNode
  friend class LinkedList;

  T data;          // node data
  ListNode* next;  // pointer to the next node      

  // constructor
  ListNode (T item, ListNode* pListNode = NULL)
    : data(item), next(pListNode) {}

template <class T>
class LinkedList
  // constructor
  LinkedList () : head(NULL) {}
  LinkedList (T item) : head (new ListNode<T> (item, NULL)) {}

  // destructor
  ~LinkedList ();

  ListNode<T>* head;     // pointer to the first node of list


Question by:screwdriver
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6371639
There are just two problems, both to do with declarations,
because of the ordering here, you need to declare the fact that LinkedList is a class (so that the Node class can see it), so add:

template <class T>
class LinkedList;

at the top of the file (before the node class),  and the friend declaration needs a template argument, so change the friend line to:

friend class LinkedList<T>;
LVL 30

Expert Comment

ID: 6371691
Please list the errors that you need help with, so we're sure we cover them all.

Expert Comment

ID: 6371754
extension of ur source file is .cpp?
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!


Expert Comment

ID: 6371896
With the source code above, the only errors are those I mentioned - accoriding to VC++ anyway.  One minor thing is that 'NULL' is not defined by default and should either be defined or (better) replaced by 0.

Author Comment

ID: 6375101
jason, post your comment as an answer. I'll give the point to you.

daknight, no.. it's .C, i am using gnu c++ compiler.. and I thought .cpp is only in VC++, is it??

Author Comment

ID: 6375105
the NULL thing was my fault. I just need to put #include <iostream.h> and it  fixed the problem.

Accepted Solution

jasonclarke earned 100 total points
ID: 6379368
Answer in response to screwdrivers request...

Expert Comment

ID: 6806117

These questions are still open and our records show you logged in recently. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days. Please note that the recommended minimum for an "Easy" question is 50 points.

EXPERTS: Please leave your thoughts on this question here.


Community Support Moderator
Experts Exchange

Expert Comment

ID: 6901312

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
Thank you everyone.
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.

Expert Comment

ID: 7024314
Force accepted

** Mindphaser - Community Support Moderator **

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

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