expected unqualified-id before '}' error on LinkedArray
Mar 13, 2017 at 4:50pm UTC
I'm trying to create a header for my LinkedArray class and I keep getting this error.. I can't pinpoint the issue, so any help would be great. Usually when it's this type of error, it's just a small thing I'm missing that would fix the code, I'm hoping that is the case here!
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
#include <vector>
using namespace std;
template <class myUnit>
class LinkedArray
{
public :
template <typename T>
struct Node
{
T data;
Node *here;
int key;
bool inUse;
Node():data(T()), key(-1), inUse(false ), here(NULL){}
};
static myUnit dummy;
/// end here
LinkedArray();
virtual ~LinkedArray();
LinkedArray(const LinkedArray<myUnit> & copy);
myUnit operator [](int index) const ;
myUnit & operator [](int index);
int CAPACITY() const {return CAP;}
int Size() const {return size;}
bool containsKey(int index) const ;
void DeleteKey(int index);
void Clear();
vector<int > Keys() const ;
private :
Node<myUnit> * begin;
Node<myUnit> * end;
int CAP;
int size;
void del();
void Copy(const LinkedArray<myUnit> & copy);
};
template <class myUnit>
myUnit LinkedArray<myUnit>::dummy = myUnit();
template <typename myUnit>
LinkedArray<myUnit>::LinkedArray()
:begin(NULL), end(NULL), CAP(0), size(0){}
template <typename myUnit>
LinkedArray<myUnit>::LinkedArray (const LinkedArray<myUnit> & copy)
:begin(NULL), end(NULL), CAP(0), size(0){ Copy(copy);}
template <class myUnit>
LinkedArray<myUnit>::~LinkedArray(){del();}
template <class myUnit>
LinkedArray<myUnit> & LinkedArray<myUnit>::operator =( const LinkedArray<myUnit> & copy )
{
if ( this != © )
{
del();
Copy(copy);
}
return *this ;
}
template <typename myUnit>
myUnit LinkedArray<myUnit>::operator [](int index) const
{
if (index < 0 || index > CAP)
return dummy;
Node<myUnit> *const prePtr = &begin;
while ( *prePtr != NULL && (*prePtr)->key < index)
prePtr = &((*prePtr)->next);
if ( *prePtr == NULL)
return dummy;
else if ( (*prePtr)->key > index)
return dummy;
else
return (*prePtr)->data;
}
template <class myUnit>
{
if (index < 0)
return dummy;
if (index >= CAP)
{
if (end == NULL) begin = end = new Node<myUnit>;
else
{
end->next = new Node<myUnit>;
end = end-> next;
}
end->key = index;
end->inUse = true ;
size++
CAP = index+1;
return end->data;
}
Node<myUnit> ** prePtr = &begin;
while ( (*prePtr) != NULL && (*prePtr)->key < index)
prePtr = &((*prePtr)-> next);
Node<myUnit> *aNode = NULL;
if ( (*prePtr)->key > index)
{
aNode = new Node<myUnit>
aNode->key= index;
aNode->next = *prePtr;
*prePtr = aNode;
}
else
{
aNode = *prePtr;
}
if ( aNode->inUse == false )
{
size++;
aNode -> inUse = true ;
}
return aNode->data;
}
template <typename myUnit>
void LinkedArray<myUnit>::Copy (const LinkedArray<myUnit> & copy)
{
Node<myUnit> * cPtr = copy.begin;
Node<myUnit> * iPtr = NULL;
while (cPtr != NULL);
{
if (cPtr == copy.begin)
{
begin = new Node<myUnit>;
iPtr = begin;
}
else
{
iPtr -> next = new Node<myUnit>;
iPtr = iPtr -> next;
}
iPtr->dats=cPtr->data;
iPtr->inUse = cPtr->inUse;
iPtr->key = cPtr->key;
if (cPtr == copy.end)
end = iPtr;
cPtr = cPtr->next;
}
size = copy.size;
CAPACITY = copy.CAP;
}
template <class myUnit>
bool LinkedArray<myUnit>::containsKey(int index) const
{
if (index <0 || index>= CAP)
return false ;
Node<myUnit> const *nPtr = begin;
while (nPtr != NULL && nPtr->key < index)
nPtr = nPtr->next;
if (nPtr != NULL)
if (nPtr->key == index)
if (nPtr->inUse==true )
return true ;
return false ;
}
template <typename myUnit>
void LinkedArray<myUnit>::DeleteKey(int index)
{
if (index >= 0 && index < CAP)
{
Node<myUnit> *nPtr = begin;
while (nPtr != NULL && nPtr->key < index)
nPtr = nPtr->next;
if (nPtr != NULL && nPtr->key == index)
{
if (nPtr->inUse == true )
size--;
nPtr->inUse = false ;
}
}
}
template <typename myUnit>
void LinkedArray<myUnit>::del()
{
Node<myUnit> * point = begin;
while (begin!=NULL)
{
begin = begin->next;
delete point;
point = begin;
}
begin = end = NULL;
CAP = size = 0;
}
template <class myUnit>
void LinkedArray<myUnit>::Clear()
{
Node<myUnit> * nPtr = begin;
while (nPtr!=NULL)
{
nPtr -> inUse = false ;
nPtr = nPtr -> next;
}
size = 0;
}
Thanks for any help you can provide CPLUSPLUS forums!
Mar 13, 2017 at 4:56pm UTC
1 2
template <class myUnit>
{
lines 89-90 what is this - a template class / struct / function? you left that out
btw I'm getting another error over here:
63|error: no 'LinkedArray<myUnit>& LinkedArray<myUnit>::operator=(const LinkedArray<myUnit>&)' member function declared in class 'LinkedArray<myUnit>' |
perhaps this dropped out during copy/paste
Last edited on Mar 13, 2017 at 4:57pm UTC
Topic archived. No new replies allowed.