Reputation: 10026
So I'm currently implementing a queue as a singly linked list. Everything is going fine, but the compiler is flagging me at my dequeue method.
This is what Visual Studio barks at me:
error C2065: 'Removed' : undeclared identifier
Here is my dequeue method which is supposed to return the value that was just removed from the queue:
template <typename Type>
Type QueueLinked<Type>::deque() {
if (queueFront == 0) {
cout << "Queue is empty! There's nothing to remove!" << endl;
} else {
nodeType<Type> *temp;
temp = queueFront;
queueFront = queueFront->next;
Type Removed = temp->dataItem;
delete temp;
if (queueFront == 0) {
queueRear = 0;
}
}
return Removed;
}
Here is my node struct:
template <typename Type>
struct nodeType {
Type dataItem;
nodeType<Type> *next;
};
This seems like an extremely simple error to have, but I'm not seeing what is causing this. Hopefully I'm not being too dumb, but it wouldn't be the first time.
Upvotes: 1
Views: 656
Reputation: 13925
You declare it in the else
block, of course it is undeclared outside of it. Declare it before the if
.
Try this way:
template <typename Type>
Type QueueLinked<Type>::deque() {
Type Removed;
if (queueFront == 0) {
cout << "Queue is empty! There's nothing to remove!" << endl;
} else {
nodeType<Type> *temp;
temp = queueFront;
queueFront = queueFront->next;
Removed = temp->dataItem;
delete temp;
if (queueFront == 0) {
queueRear = 0;
}
}
return Removed;
}
Upvotes: 1