Template Class: LNK2019
Jul 8, 2010 at 11:34pm UTC
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
#include <iostream>
using namespace std;
template <class ItemType>
class ArrayList
{
public :
enum SortOrder {ASCENDING, DESCENDING};
private :
typedef ItemType* pItemType;
typedef pItemType* ppItemType;
ppItemType items;
size_t count;
// Finds the index of the next item in order.
size_t FindNextIndex(const int start, const SortOrder order) const ;
// Swaps the two items at the given indices.
void SwapItems(const size_t index1, const size_t index2);
// Copies the parameter list.
void CopyList(const ArrayList& copyArrayList);
// Deletes all allocated memory.
void DeleteList();
// Displays the items in the array, one per line.
friend ostream& operator <<(ostream& out, const ArrayList& anArrayList);
public :
// Sets items to NULL and count to 0.
ArrayList();
// Initializes object to the parameter object.
ArrayList(const ArrayList& copyArrayList);
// Deletes all allocated memory.
~ArrayList();
// Assigns the parameter object to the current object.
ArrayList operator =(const ArrayList<ItemType>& assignArrayList);
// Adds and item and returns added item. Array is resized by one.
pItemType Add(pItemType anItem);
// Removes the item at the given index. Array is resize by one.
void RemoveItemAtIndex(const size_t index);
// Returns the item at the given index.
pItemType ItemAtIndex(const size_t index) const ;
// Returns the size of the array.
size_t Count() const ;
// Sorts array in either ascending/descending order.
void Sort(const SortOrder order);
};
// Finds the index of the next item in order.
template <class ItemType>
size_t ArrayList<ItemType>::FindNextIndex(const int start, const SortOrder order) const
{
size_t next = start;
if (order == ASCENDING)
{
for (size_t i = start+1; i < count; i++)
{
if (*items[next] > *items[i])
next = i;
}
}
else
{
for (size_t i = start+1; i < count; i++)
{
if (*items[next] < *items[i])
next = i;
}
}
return next;
}
// Swaps the two items at the given indices.
template <class ItemType>
void ArrayList<ItemType>::SwapItems(const size_t index1, const size_t index2)
{
ItemType temp = *items[index1];
*items[index1] = *items[index2];
*items[index2] = temp;
}
// Copies the parameter list.
template <class ItemType>
void ArrayList<ItemType>::CopyList(const ArrayList<ItemType>& copyArrayList)
{
count = copyArrayList.Count();
items = new pItemType[copyArrayList.Count()];
for (size_t i = 0; i < count; i++)
{
items[i] = new ItemType(*copyArrayList.ItemAtIndex(i));
}
}
// Deletes all allocated memory.
template <class ItemType>
void ArrayList<ItemType>::DeleteList()
{
for (size_t i = 0; i < Count(); i++)
{
delete items[i];
items[i] = NULL;
}
delete items;
items = NULL;
}
// Displays the items in the array in one line.
template <class ItemType>
ostream& operator <<(ostream& out, const ArrayList<ItemType>& anArrayList)
{
for (size_t i = 0; i < anArrayList.Count(); i++)
{
out << *anArrayList.items[i] << " " ;
}
return (out);
}
// Sets items to NULL and count to 0.
template <class ItemType>
ArrayList<ItemType>::ArrayList()
{
items = NULL;
count = 0;
}
// Initializes object to the parameter object.
template <class ItemType>
ArrayList<ItemType>::ArrayList(const ArrayList<ItemType>& copyArrayList)
{
CopyList(copyArrayList);
}
// Deletes all allocated memory.
template <class ItemType>
ArrayList<ItemType>::~ArrayList()
{
DeleteList();
}
// Assigns the parameter object to the current object.
template <class ItemType>
ArrayList<ItemType> ArrayList<ItemType>::operator =(const ArrayList<ItemType>& assignArrayList)
{
if (items != assignArrayList.items)
{
DeleteList();
CopyList(assignArrayList);
}
return (*this );
}
// Adds and item and returns added item. Array is resized by one.
template <class ItemType>
typename ArrayList<ItemType>::pItemType ArrayList<ItemType>::Add(pItemType anItem)
{
if (items == NULL)
{
count++;
items = new pItemType[count];
items[count-1] = anItem;
}
else
{
ArrayList temp;
temp.count = Count()+1;
temp.items = new pItemType[temp.count];
for (size_t i = 0; i < temp.count-1; i++)
{
temp.items[i] = new ItemType(*(*this ).ItemAtIndex(i));
}
temp.items[temp.count-1] = new ItemType(*anItem);
(*this ).DeleteList();
(*this ).CopyList(temp);
}
return items[count-1];
}
// Removes the item at the given index. Array is resize by one.
template <class ItemType>
void ArrayList<ItemType>::RemoveItemAtIndex(const size_t index)
{
for (size_t i = index; i < Count(); i++)
{
items[i] = items[i+1];
}
count--;
}
// Returns the item at the given index.
template <class ItemType>
typename ArrayList<ItemType>::pItemType ArrayList<ItemType>::ItemAtIndex(const size_t index) const
{
return items[index];
}
// Returns the size of the array.
template <class ItemType>
size_t ArrayList<ItemType>::Count() const
{
return count;
}
// Sorts array in either ascending/descending order.
template <class ItemType>
void ArrayList<ItemType>::Sort(const SortOrder order)
{
for (size_t i = 0; i < Count(); i++)
{
SwapItems(i, FindNextIndex(i, order));
}
}
#endif
Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class ArrayList<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$ArrayList@H@@@Z) referenced in function _main p04.obj
I'm getting this linking error, not sure why. Any help?
Jul 9, 2010 at 5:10am UTC
It is the way the friend function has been defined.
In the class definition do this:
1 2 3
// Displays the items in the array, one per line.
template <class T>
friend ostream& operator <<(ostream& out, const ArrayList<T>& anArrayList);
and in the definition:
1 2 3 4 5 6 7 8 9 10
// Displays the items in the array in one line.
template <class T>
ostream& operator <<(ostream& out, const ArrayList<T>& anArrayList)
{
for (size_t i = 0; i < anArrayList.Count(); i++)
{
out << *anArrayList.items[i] << " " ;
}
return (out);
}
Topic archived. No new replies allowed.