undeclared declaration in friend class
Oct 25, 2009 at 5:36pm UTC
Im getting an error while overloading << that "cur" is coming up undeclared.I know that the function code is right. i just dont know what my problem is.
header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#ifndef LList_h_
#define LList_h_
#include <iostream>
template <class itemtype>
class llist
{
template <itemtype>
friend std::ostream& operator <<(std::ostream & , llist<itemtype> &);
private :
struct nodetype
{
itemtype item;
nodetype *next;
};
nodetype *first, *last;
public :
llist();
void firstinsert(itemtype);
void frontinsert(itemtype);
void endinsert(itemtype);
void inorderinsert(itemtype);
bool search(itemtype);
bool remove (itemtype);
bool listempty();
itemtype firstitem();
itemtype lastitem();
~llist();
};
#endif
implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#include "LList.h"
template <class itemtype>
llist<itemtype>::llist()
{
first=last=0;
}
template <class itemtype>
void llist<itemtype>::firstinsert(itemtype x)
{
first= new nodetype;
last= first;
first->item =x;
first->next = 0;
}
template <class itemtype>
void llist<itemtype>::frontinsert(itemtype x)
{
nodetype *newnode;
newnode= new nodetype;
newnode->item =x;
newnode->next=first;
first=newnode;
}
template <class itemtype>
void llist<itemtype>::endinsert(itemtype x)
{
nodetype *newnode;
newnode= new nodetype;
newnode->item =x;
newnode->next=0;
last->next=newnode;
last=newnode;
}
template <class itemtype>
void llist<itemtype>::inorderinsert(itemtype x)
{
nodetype *cur;
nodetype *prev;
nodetype *newnode;
newnode= new nodetype;
newnode->item =x;
newnode->next=0;
if (x<=first->item)
{
newnode->next=first;
first=newnode;
}
else if (x>=last->item)
{
last->next=newnode;
last=newnode;
}
else
{
prev=first;
cur=first->next;
while (x>cur->item)
{
prev=cur;
cur=cur->next;
}
}
}
template <class itemtype>
bool llist<itemtype>::listempty()
{
return (first==0);
}
template <class itemtype>
itemtype llist<itemtype>::firstitem()
{
return first->item;
}
template <class itemtype>
itemtype llist<itemtype>::lastitem()
{
return last->item;
}
template <class itemtype>
bool llist<itemtype>::search(itemtype x)
{
nodetype *cur;
cur=first;
while ((cur!=0)&&(x!=cur->item))
{
cur=cur->next;
}
return (cur!=0);
}
template <class itemtype>
llist<itemtype>::~llist()
{
nodetype *delnode;
while (first!=0)
{
delnode=first;
first=first->next;
delete delnode;
}
}
template <class itemtype>
bool llist<itemtype>::remove (itemtype x)
{
nodetype *cur, *prev;
nodetype *delnode;
prev=0;
cur=first;
while ((cur!=0)&&(x!=cur->item))
{
prev=cur;
cur=cur->next;
if (cur==0)
return 0;
else
delnode=cur;
if (prev==0)
{
first=first->next;
}
else
{
prev->next=cur->next;
if (cur==last)
last=prev;
}
delete delnode;
return 1;
}
}
template <class itemtype>
std::ostream& operator <<(std::ostream &output,llist<itemtype> &l)
{
llist<itemtype>::nodetype *cur;
cur=l.first;
while (cur!=0)
{
output<<cur->item<<"|" ;
cur=cur->next;
}
return output;
}
any help is appreciated
Oct 25, 2009 at 7:58pm UTC
See if this solves the problem :
1 2 3 4 5 6 7 8 9 10 11 12
template <class itemtype>
std::ostream& operator <<(std::ostream &output,llist<itemtype> &l)
{
typename llist<itemtype>::nodetype *cur; //notice use of the typename keyword
cur=l.first;
while (cur!=0)
{
output<<cur->item<<"|" ;
cur=cur->next;
}
return output;
}
Last edited on Oct 25, 2009 at 7:59pm UTC
Oct 25, 2009 at 8:07pm UTC
thanks dude,worked perfectly.
Topic archived. No new replies allowed.