473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

friends template

Hi All,
I encoutering this problem when trying to compile my code.
list.h:10: error: ‘List’ is not a template
It seems like the problem comes from the declaration of my friend template class List in template class ListNode.
This my lis.h file
//-----------------------------------------------------------------------------
// class ListeNode
// contains the operations for class List
//-----------------------------------------------------------------------------
template <class T>
class ListNode{
template <class U>
friend class List;
public:
ListNode<T>(T & t, ListNode<T> * p)
:data(t),next(p){}
protected:
T data; //record the data
ListNode<T> * next; //pointeur to the next node
};
//-----------------------------------------------------------------------------
// class Liste
// dynamic array
//-----------------------------------------------------------------------------

template <class U>
class List{
public:
List():first(0){}
~List();

public:
void insert(U t);//includes a new data in the head of the list
void remove(U &t);//remove the first element of the list
int isEmpty() {return first = 0;}
void print();

protected:
ListNode<U> * first;
ListNode<U> * newNode(U & t, ListNode<U> * p)
{
ListNode<U> * q = new ListNode<U>(t,p);
return q;
}

};
#endif
this is the implementation of list.h
#include "list.h"

template <class U>
List::~List()
{
ListNode<U> * tmp;
for (ListNode<U> * p =first; p; ){
tmp = p;
p = p->next;
delete tmp;
}
first = 0;
}
template <class U>
void List<U>::insert(U t)
{
ListNode<U> * p = newNode(t, first);
first = p;
}
template <class U>
int List::remove(U & t)
{
if (isEmpty()) return 0;
t = first->data;
ListNode<U> * p = first;
first = first->next;
delete p;
return 1;
}
template <class U>
void List<U>::print()
{
for (listNode<U> * p=first; p; p=p->next)
cout << p->datat << "->";
cout << "*\n";
}
and this is my main function
#include <iostream.h>
#include <string>
#include "list.h"
int main(){
List<int> x;
x.insert(5);
x.insert(2);
x.insert(3);
x.insert(4);
x.print();
int y;
x.remove(y);
cout << "Removed: " << y << endl;
x.print();
return 0;
}
Is any one who can help?
thank you.
Guy
Oct 31 '06 #1
1 1400
Banfa
9,065 Expert Mod 8TB
I expect the problem is at the line

friend class List;

although you have not clearly indicated this.

This is because when ListNode is declared there has not yet been a declaration of List. However you can not move the declaration of list before the declaration of ListNode because List uses ListNode (well you could but it wouldn't get rid of the problem just move it to a different line).

What you need to do is forward declare the class List. A forward declaration tells the compiler that there will be a class (or structure or enum or union) of a given type but it is not declared yet. This provides enough information for the compiler to create pointers to that class or use it in a friend statement. A forward declaration looks like this

template <class U>
class List;

Put it in the header before ListNode is declared.
Oct 31 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gianni Mariani | last post by:
I have 2 distinct template classes which co-operate, hence are friends. However, I can't seem to figure out what syntax to use to make this work. What is the right(tm) way to write a friend...
10
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
5
by: Pete C. | last post by:
I'm trying to make a templated function a friend like this: class cls { ... friend cls func<> (const cls& a, const cls& b); }; template< template<class> class Op> cls func (const cls& a,...
1
by: Tom McCallum | last post by:
Hi, Can someone please tell me the correct syntax (if its possible of course) to specify an output stream operator for a templated class so that I dont need to write the same function for all...
4
by: BigMan | last post by:
This code does not compile on g++ 3.4.2. I want the template argument to be a friend of the template class. What syntax should I use to define such relationship? template< typename t > class t2...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
7
by: Noah Roberts | last post by:
template < typename V > class Outer { public: template < typename T > class Inner { private: template <classfriend class Inner; };
9
by: Klaas Vantournhout | last post by:
Hi all, I have a question about friends functions of a template class. To make it simple, I would like to do something like this. Assume that I have a class foo with template T ...
2
by: Dave Rudolf | last post by:
Hey all, So I have a template class whose only template parameter is a type that the class is to manage. It's actually an implementation of the common Singleton design pattern. The class looks...
12
by: siddhu | last post by:
Hello, would you suggest to me, why gcc 3.3.3 can not compile this: template<class T> class Base { Base(){} friend T; };
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.