Link to home
Start Free TrialLog in
Avatar of screwdriver
screwdriver

asked on

Help... Template Question for my class

When I want to compile the following code, I got a bunch of errors that I don't understand... please help.

main.C
------
#include "llist.h"
#include <iostream.h>

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

llist.h
-----------
#ifndef _LLIST_H_
#define _LLIST_H_

template <class T>
class ListNode
{
  friend class LinkedList;

 private:
  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
{
 public:
  // constructor
  LinkedList () : head(NULL) {}
  LinkedList (T item) : head (new ListNode<T> (item, NULL)) {}

  // destructor
  ~LinkedList ();

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

#endif


Avatar of jasonclarke
jasonclarke

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>;
Avatar of Axter
Please list the errors that you need help with, so we're sure we cover them all.
extension of ur source file is .cpp?
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.
Avatar of screwdriver

ASKER

jason, post your comment as an answer. I'll give the point to you.
Thanks!

daknight, no.. it's .C, i am using gnu c++ compiler.. and I thought .cpp is only in VC++, is it??
the NULL thing was my fault. I just need to put #include <iostream.h> and it  fixed the problem.
ASKER CERTIFIED SOLUTION
Avatar of jasonclarke
jasonclarke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
screwdriver,

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.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20084627
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20166343
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20183351
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20195107
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20266182
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20266180

EXPERTS: Please leave your thoughts on this question here.

Thanks,

Netminder
Community Support Moderator
Experts Exchange
SECOND REQUEST!

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.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.
https://www.experts-exchange.com/questions/Q.20084627.html
https://www.experts-exchange.com/questions/Q.20195107.html
https://www.experts-exchange.com/questions/Q.20183351.html
https://www.experts-exchange.com/questions/Q.20269019.html
https://www.experts-exchange.com/questions/Q.20275369.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20166343.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  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 https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

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.
 
Moondancer
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.
Force accepted

** Mindphaser - Community Support Moderator **