overloading << with template class
I had to create a list class and add a "<<" function.
this is the code I wrote:
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 147 148 149 150 151 152 153
|
template <class T>
class Node
{
private:
T val;
Node <T> * next;
public:
Node <T> (const T & x)
{
val = x;
next = 0;
}
void setVal (const T& x) { val = x; }
T getVal () { return val; }
void setNext (Node <T> * x) { next = x; }
Node <T> * getNext () { return next;}
};
template <class T>
class List
{
private:
Node <T> * n;
int size;
public:
List<T> () { n = 0, size = 0; }
Node <T> * add (Node <T> * pos,const T & x)
{
Node <T> * temp = new Node <T> (x);
if (size == 0)
{
n = temp;
pos = n;
}
else
{
if (pos == 0 && size!= 0)
{
temp -> setNext(n);
n = temp;
pos = n;
}
else
{
Node <T> * p = n;
while ( p != pos)
p = p -> getNext();
p -> setNext(temp);
pos = p -> getNext();
}
}
++size;
return pos;
}
Node <T> * remove (Node <T> * pos)
{
if (pos == n && size == 1)
{
--size;
n = 0;
return 0;
}
int counter = 1;
Node <T> * p = n;
while ( (p -> getNext()) != pos)
{
++counter;
p = p -> getNext();
}
if (pos -> getNext() == 0)
p -> setNext(0);
p -> setNext ( pos ->getNext() );
--size;
return p -> getNext();
}
Node <T> * search (Node <T> * pos)
{
Node <T> * p = n;
while ( p != pos )
{
p = p -> getNext();
}
return p;
}
int getSize () { return size; }
template <typename T1>
friend ostream& operator<< (std :: ostream& , const List<T1>& );
};
template <typename T>
ostream& operator << (std:: ostream& os, const List<T> & lst)
{
os << "[" << lst.n -> getVal();
Node <T> * p = lst.n -> getNext ();
for (int i = 1; i < lst.size; ++i)
{
os << " , " << p ->getVal();
p = p -> getNext();
}
os << "]";
return os;
}
int main ()
{
Node <char> * a;
Node <char> * b;
List <char> * lst = new List <char> ();
a = lst -> add(0,'a');
a = lst -> add (a, 'b');
b = a;
a = lst -> add (a, 'D');
a = lst -> remove (b);
cout << a -> getVal() << endl;
cout << lst;
return 0;
}
|
the last line (cout << lst) prints a random number (I think it's the os address..?). can anyone tell me what's wrong with it?
lst
is a pointer. So you're printing the contents of a pointer, which is naturally an address.
You are overusing pointers in my opinion. You also have a memory leak because you must pair each
new with a
delete.
So you must dereference the pointer by using the asterisk operator, and also
delete lst;
in the end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int main ()
{
Node<char> *a;
Node<char> *b;
List<char> *lst = new List<char>();
a = lst->add(0,'a');
a = lst->add(a, 'b');
b = a;
a = lst->add(a, 'D');
a = lst->remove(b);
cout << a->getVal() << endl;
cout << *lst;
delete lst;
return 0;
}
|
Also I would suggest you compact your code a little.
Last edited on
thanks!
Topic archived. No new replies allowed.