[Dev-C++] help needed for friends in template class
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: bhadram r. <bha...@ya...> - 2005-08-17 05:51:18
|
Dear friends,
I'd problem with usage of friends in templates in vc++ compiler. Please any one can help me.
I want to make the Node class as frined to List class to access the private variables of Node class int list class but it is giving the compilation errors like this. With out the frinds class definition it is working fine. friends class defintion is line 4 of Node class. Please write the solution as it is sucking me.Waiting for your help.
yours truly,
bhadram
Compiling...
list.cpp
d:\cpppractice\listproject\list1.h(6) : error C2059: syntax error : '<'
d:\cpppractice\listproject\list1.h(17) : see reference to class template instantiation 'Node<T>' being compiled
d:\cpppractice\listproject\list1.h(6) : error C2238: unexpected token(s) preceding ';'
d:\cpppractice\listproject\list1.h(17) : see reference to class template instantiation 'Node<T>' being compiled
d:\cpppractice\listproject\list1.h(32) : error C2989: 'List' : template class has already been defined as a non-template class
d:\cpppractice\listproject\list1.h(6) : see declaration of 'List'
d:\cpppractice\listproject\list1.h(36) : error C2059: syntax error : '<'
d:\cpppractice\listproject\list1.h(44) : error C2039: 'insert' : is not a member of '`global namespace''
d:\cpppractice\listproject\list1.h(45) : error C2143: syntax error : missing ';' before '{'
d:\cpppractice\listproject\list1.h(45) : error C2447: missing function header (old-style formal list?)
d:\cpppractice\listproject\list1.h(50) : error C2954: template definitions cannot nest
d:\cpppractice\listproject\list1.h(52) : error C2039: 'print' : is not a member of 'List'
d:\cpppractice\listproject\list1.h(32) : see declaration of 'List'
d:\cpppractice\listproject\list.cpp(7) : error C2143: syntax error : missing ';' before '<'
d:\cpppractice\listproject\list.cpp(7) : error C2143: syntax error : missing ';' before '<'
d:\cpppractice\listproject\list.cpp(10) : error C2065: 'l' : undeclared identifier
d:\cpppractice\listproject\list.cpp(10) : error C2228: left of '.insert' must have class/struct/union type
d:\cpppractice\listproject\list.cpp(12) : error C2228: left of '.print' must have class/struct/union type
Error executing cl.exe.
list.obj - 14 error(s), 0 warning(s)
the code is the header file:
template<class T>
class Node
{
friend class List<T>;
private:
T data;
Node<T>* next;
public:
Node(T v,Node<T>* n)
{
data=v;
next=n;
}
};
template<class T>
class List
{
private:
public:
Node<T>* first;
Node<T>* last;
List();
void insert(T v);
void print();
};
template<class T>
List<T>::List()
{
first='\0';
last='\0';
}
template<class T>
void List<T>::insert(T v)
{
Node* n=new Node(v,first);
first=n;
}
template<class T>
void List::print()
{
for(Node* p=first;p;p=p->next)
{
cout<<p->data<<endl;
}
}
the cpp program:
# include <iostream.h>
# include<List1.h>
void main()
{
List<int> l;
for(int i=0;i<5;i++)
{
l.insert(i);
}
l.print();
}
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com |