Compilation fails with "expected unqualified-id" on "using" - c++

I need some help making this program for class. We are working with g++ (linux? its through putty on a server) I am having a lot of issue with this linked list. The current error that it is giving me is
queue.cpp:2: error: expected unqualified-id before âusingâ
Wondering if anyone could help me figure it out. A little bit of searching shows that the problem seems to be in the #define somewhere? The error shows that it is in the .cpp file but i think it is in the .h file. Also if you could give me any programing advise about anything that seems off, wrong or if there a better way of doing it.
the queue.h file below
#ifndef QUEUE_H
#define QUEUE_H
template <class Object>
class Queue
{
public:
Queue();
Queue(const Queue& a_queue);
Queue& operator =(const Queue& rhs);
bool enqueue(const Object& d);
bool dequeue(const Object& d);
bool isEmpty() const;
~Queue();
private:
struct ListNode
{
Object obj;
ListNode *next;
};
ListNode *head;
}
#endif //Queue_H
#include "queue.cpp" //include queue.cpp with file
the queue.cpp file here.
#include <iostream>
using namespace std;
template <class Object>
Queue<Object>::Queue()
{
head = NULL;
}
template <class Object>
Queue<Object>::Queue(const Queue<Object>& a_queue)
{
head=NULL;
*this=a_queue;
}
template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
ListNode *nodePtr;
nodePtr = rhs.head;
if(this != rhs){
this.head = NULL;
while(nodePtr != NULL){
this.enqueue(nodePtr->obj);
nodePtr = nodePtr->next;
}
}
}
template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode;
previousNode = NULL;
nodePtr = NULL;
newNode = new ListNode;
newNode->obj = d;
newNode->next = NULL;
if(isEmpty){
head = newNode;
return true;
}
else{
nodePtr = head;
previousNode = NULL;
while(nodePtr != NULL){
previousNode = nodePtr;
nodePtr=nodePtr->next;
}
if(previousNode->next == NULL){
previousNode->next = newNode;
return true;
}
else
return false;
}
}
template <class Object>
bool Queue<Object>::dequeue (const Object& d) //Dequeue
{
ListNode *nodePtr;
if(!head)
return false;
else{
if(head->next != NULL){
nodePtr = head;
d=nodePtr->obj;
head = head->next;
delete nodePtr;
}else
delete head;
return true;
}
}
template <class Object>
bool Queue<Object>::isEmpty() const //Check if Empty
{
if(!head)
return true;
else
return false;
}
template <class Object>
Queue<Object>::~Queue() //Destructor
{
Object temp;
while (head != NULL)
dequeue (temp);
}

Don't include your implementation file from the header. Include the header from the implementation file.
I don't see you say "using namespace std" in the header in your code, but you say in your comment that you do that. Don't. Never say using namespace in a header file.
Place all template definitions in the header.
You're missing a semicolon in your class definition in the header.
Your assignment operator is not exception-safe. Make sure your copy-constructor is correct and then use the copy & swap idiom.
Your copy-constructor is incorrect. If you want to support lazy copy (copy on write) then that's fine, but you're missing the actual deep copy operation. Be extra-careful with the copy constructor, because it extremely important you get it right.

You need a semicolon after your class declaration in the header.
class Queue
{
public:
Queue();
Queue(const Queue& a_queue);
Queue& operator =(const Queue& rhs);
bool enqueue(const Object& d);
bool dequeue(const Object& d);
bool isEmpty() const;
~Queue();
private:
struct ListNode
{
Object obj;
ListNode *next;
};
ListNode *head;
};

Related

How do I properly use nested classes in my hpp file and cpp file?

I am trying to implement a singly linked list with an iterator as a nested class of the linked list class. I am getting a lot of errors that I think have to do with working with my iterators. I have spent a few hours researching this, but unfortunately a lot of the solutions include templates and I'm not able to use those. Every time I fix one problem, a new one shows up.
I'm getting the below errors in my cpp file:
Use of undeclared identifier 'current'
Expected ';' after top level declarator (I'm very confused on why I'm getting this because I thought I delared iterator in my hpp file
Overloaded 'operator++' must have at least one parameter of class or enumeration type (we're not supposed to have parameters for our overloaded operators in this case, right? I just want to iterate so that I am now pointing to the next item in a linked list
Invalid use of 'this' outside of a non-static member function
'Iterator' is not a class, namespace, or enumeration
I will continue to do more research and report back here if I figure out what is wrong with my code to hopefully help if others have the same question.
Thanks so much in advance!
Here is my hpp file:
#ifndef linkedlist_h
#define linkedlist_h
class LinkedList {
public:
struct Node {
//Node(Node *aNext=nullptr) : next(aNext) {}
int value; //this is the value you want to save
Node *next; //this points to the next node in the list (or nullptr)
};
//friend class Iterator; //do Ineed this even though it's a nested class
Node *root;
//---------------------------------------------------------------
//add a NESTED Interator class...
class Iterator {
public:
Iterator(); //default constructor
Iterator(Node* aNode);//constructor
~Iterator(); //dtor
Iterator operator++();
Iterator operator++(int);
bool operator==(const Iterator &anIterator);
bool operator!=(const Iterator &anIterator);
int operator*();
operator Node*();
Node *current; //do I need to put LinkedList since it's a nested class?
//add all the necessary operators
protected:
};
//--------------------------------------------------------------
LinkedList(); //default constructor...
LinkedList(const LinkedList& aCopy); //copy ctor
~LinkedList(); //dtor
LinkedList& operator=(const LinkedList& aCopy); //assignment operator
void append(int value);
void prepend(int value);
void remove(int value);
int size(); //needs to return unsigned int????
Iterator begin();
Iterator end();
Iterator find(int value);
protected:
};
#endif /* linkedlist_h */
Here is my cpp file:
#include <stdio.h>
#include "LinkedList.hpp"
//class Iterator;
LinkedList::LinkedList() {
root=nullptr;
}
LinkedList::LinkedList(const LinkedList& aCopy){ //copy ctor
Node *temp=aCopy.root;
Node *newNode = new Node;
root=newNode;
while (temp != nullptr){
newNode-> value=temp->value;
temp=temp->next;
if (temp !=nullptr){
newNode->next=new Node;
newNode=newNode->next;
}
else{ newNode->next=nullptr;}
}
}
LinkedList& LinkedList::operator=(const LinkedList &aCopy){ //assignment operator
while(root!=nullptr){
Node* oneBefore= root;
root =root->next;
delete oneBefore;
}
Node *newNode= new Node;
Node *temp=aCopy.root;
root=newNode;
while(temp!=nullptr){
newNode->value=temp->value;
temp=temp->next;
if(temp!=nullptr){
newNode->next=new Node;
newNode=newNode->next;
}
else{newNode->next=nullptr;}
}
return *this;
}
LinkedList::~LinkedList(){ //dtor
Node* oneBefore = nullptr;
while(root!=nullptr){
oneBefore=root;
root=root->next;
delete oneBefore;
}
}
void LinkedList::append(int value){
Node* newNode=new Node;
newNode->value=value;
if(root!=nullptr){
Node* temp = root;
while (temp->next !=nullptr){
temp=temp->next;
}
newNode->next=nullptr;
temp->next=newNode;
}
if(root==nullptr){
newNode->next=nullptr;
root=newNode;
}
}
void LinkedList::prepend(int value){
Node* newNode=new Node;
newNode->value=value;
if (root!=nullptr){
newNode->next=root;
root=newNode;
}
if(root==nullptr){
root=newNode;
newNode->next=nullptr;
}
}
void LinkedList::remove(int value){
if(root==nullptr){
Node *before=nullptr;
Node *temp=root;
if(temp->value==value){
root=temp->next;
}
else{
while(temp->value!=value &&temp->next != nullptr){
before=temp;
temp=temp->next;
}
if(temp->value==value){
before->next=temp->next;
}
}
delete temp;
}
}
int LinkedList::size(){
Node* aNode = root;
int numElements=0;
while(aNode!=nullptr){
aNode=aNode->next;
numElements=numElements+1;
}
return numElements;
}
LinkedList::Iterator LinkedList::begin(){
return LinkedList::Iterator(root);
}
LinkedList::Iterator LinkedList::end(){
Node *aNode=root;
while(aNode!=nullptr){
aNode=aNode->next;
}
return LinkedList::Iterator(aNode);
}
LinkedList::Iterator Iterator(){
current=nullptr;
}
LinkedList::Iterator(Node *aNode){
current=aNode;
}
LinkedList::Iterator operator++(){//I have no idea what the difference is supposed to be between this one and the one below
current=current->next;
return *this;
}
LinkedList::Iterator& Iterator::operator=(const LinkedList::Iterator& aCopy){ //assignment operator
current=aCopy.current;
return *this;
}
bool Iterator::operator !=(const LinkedList::Iterator& aCopy){
return current != aCopy.current;
}
bool Iterator::operator==(const LinkedList::Iterator& aCopy){
return current==aCopy.current;
}
int Iterator::operator*(){
return current->value;
}
UPDATE: The suggestions below helped so much! Thank you so much! The last error I have is (I'll mark where it is shown on the compiler below):
- "Definition of implicitly declared copy assignment operator"
Updated hpp file:
#ifndef linkedlist_h
#define linkedlist_h
class LinkedList {
public:
struct Node {
//Node(Node *aNext=nullptr) : next(aNext) {}
int value; //this is the value you want to save
Node *next; //this points to the next node in the list (or nullptr)
};
//friend class Iterator; //do Ineed this even though it's a nested class
Node *root;
//---------------------------------------------------------------
//add a NESTED Interator class...
class Iterator {
public:
Iterator();//default constructor
//Iterator() : current(nullptr) {}
Iterator(Node* aNode);
//Iterator(Node* aNode): current(aNode){};//constructor
~Iterator(); //dtor
Iterator operator++();
Iterator operator++(int);
bool operator==(const Iterator &anIterator);
bool operator!=(const Iterator &anIterator);
int operator*();
operator Node*();
Node *current; //do I need to put LinkedList since it's a nested class?
//add all the necessary operators
protected:
};
//--------------------------------------------------------------
LinkedList(); //default constructor...
LinkedList(const LinkedList& aCopy); //copy ctor
~LinkedList(); //dtor
LinkedList& operator=(const LinkedList& aCopy); //assignment operator
void append(int value);
void prepend(int value);
void remove(int value);
int size(); //needs to return unsigned int????
Iterator begin();
Iterator end();
Iterator find(int value);
protected:
};
#endif /* linkedlist_h */
Updated cpp file:
#include <stdio.h>
#include "LinkedList.hpp"
//class Iterator;
LinkedList::LinkedList() {
root=nullptr;
}
LinkedList::LinkedList(const LinkedList& aCopy){ //copy ctor
Node *temp=aCopy.root;
Node *newNode = new Node;
root=newNode;
while (temp != nullptr){
newNode-> value=temp->value;
temp=temp->next;
if (temp !=nullptr){
newNode->next=new Node;
newNode=newNode->next;
}
else{ newNode->next=nullptr;}
}
}
LinkedList& LinkedList::operator=(const LinkedList &aCopy){ //assignment operator
while(root!=nullptr){
Node* oneBefore= root;
root =root->next;
delete oneBefore;
}
Node *newNode= new Node;
Node *temp=aCopy.root;
root=newNode;
while(temp!=nullptr){
newNode->value=temp->value;
temp=temp->next;
if(temp!=nullptr){
newNode->next=new Node;
newNode=newNode->next;
}
else{newNode->next=nullptr;}
}
return *this;
}
LinkedList::~LinkedList(){ //dtor
Node* oneBefore = nullptr;
while(root!=nullptr){
oneBefore=root;
root=root->next;
delete oneBefore;
}
}
void LinkedList::append(int value){
Node* newNode=new Node;
newNode->value=value;
if(root!=nullptr){
Node* temp = root;
while (temp->next !=nullptr){
temp=temp->next;
}
newNode->next=nullptr;
temp->next=newNode;
}
if(root==nullptr){
newNode->next=nullptr;
root=newNode;
}
}
void LinkedList::prepend(int value){
Node* newNode=new Node;
newNode->value=value;
if (root!=nullptr){
newNode->next=root;
root=newNode;
}
if(root==nullptr){
root=newNode;
newNode->next=nullptr;
}
}
void LinkedList::remove(int value){
if(root==nullptr){
Node *before=nullptr;
Node *temp=root;
if(temp->value==value){
root=temp->next;
}
else{
while(temp->value!=value &&temp->next != nullptr){
before=temp;
temp=temp->next;
}
if(temp->value==value){
before->next=temp->next;
}
}
delete temp;
}
}
int LinkedList::size(){
Node* aNode = root;
int numElements=0;
while(aNode!=nullptr){
aNode=aNode->next;
numElements=numElements+1;
}
return numElements;
}
LinkedList::Iterator LinkedList::begin(){
return LinkedList::Iterator(root);
}
LinkedList::Iterator LinkedList::end(){
Node *aNode=root;
while(aNode!=nullptr){
aNode=aNode->next;
}
return LinkedList::Iterator(aNode);
}
LinkedList::Iterator::Iterator() : current(nullptr) {}
LinkedList::Iterator::Iterator(Node* aNode): current(aNode){};
LinkedList::Iterator LinkedList::Iterator::operator++(){//I have no idea what the difference is supposed to be between this one and the one below
current=current->next;
return *this;
}
LinkedList::Iterator& LinkedList::Iterator::operator=(const LinkedList::Iterator& aCopy) noexcept{ //assignment operator; THIS IS WHERE I SEE AN ERROR
current=aCopy.current;
return *this;
}
bool LinkedList::Iterator::operator !=(const LinkedList::Iterator& aCopy){
return current != aCopy.current;
}
bool LinkedList::Iterator::operator==(const LinkedList::Iterator& aCopy){
return current==aCopy.current;
}
int LinkedList::Iterator::operator*(){
return current->value;
}
The first problem (as I already said as a comment) was, that the definitions of nested class methods should look like LinkedList::Iterator::Iterator() { ... } or bool LinkedList::Iterator::operator!=(...) { ... }. (I just write this here again to make it easier to find, if someone else has the same problem)
The reason why you get an error in the definition of the assignment operator is simply that you haven't declared an assignment operator for that class in your header.
As a bonus round: The difference between operator++() and operator++(int) is, that the first one is called with ++a (pre-increment), while the other one is called with a++ (post-increment). The pre-increment one should also return a reference to the same object (similar to the assingment operator), while the post-increment returns a copy of of the object as it was before the increment.

Undeclared identifier, in a node class

I have 2 files: Node.h, Node.cpp,
In Node.h, I create the prototype for the Node class. In the prototype I create a string array 'name'. In the Node.cpp class, I tried to use a function that gives 'name' a value, but i keep getting undeclared identifier even though i identified 'name' in Node.h
node.h
#include "iostream"
#include "string.h"
#include "stdafx.h"
#include "stdio.h"
template<class T>
class Node{
char name[256];
bool useable;
public:
//Constructors
Node();
Node(const T& item, Node<T>* ptrnext = NULL);
T data;
//Access to next Node
Node<T>* nextNode();
//List modification
void insertAfter(Node<T>* p);
Node<T>* deleteAfter();
Node<T>* getNode(const T& item, Node<T>* nextptr = NULL);
//Data Retrieval
char *getName();
void *setName(char[]);
bool isUsable();
};
node.cpp
#include "Node.h"
//Default Constructor
template<class T>
Node<T>::Node(){
}
//This constructor sets the next pointer of a node and the data contained in that node
template<class T>
Node<T>::Node(const T& item,Node<T>* ptrnext){
this->data = item;
this->next = ptrnext;
}
//This method inserts a node after the current node
template<class T>
void Node<T>::insertAfter(Node<T> *p){
//Links the rest of list to the Node<T>* p
p->next = this->next;
//Links the previous node to this one
this-> next = p;
}
//This method deletes the current node from the list then returns it.
template<class T>
Node<T> * Node<T>::deleteAfter(){
Node<T>* temp = next;
if(next !=NULL){
next = next->next;
}
return temp;
}
template<class T>
Node<T> * getNode(const T& item, Node<T>* nextptr = NULL){
Node<T>* newnode; //Local pointer for new node
newNode = new Node<T>(item,nextptr);
if (newNode == NULL){
printf("Error Allocating Memory");
exit(1);
}
return newNode;
}
void setName(char input[256]){
strncpy(name,input,sizeof(name));
}
I see three things immediately wrong with the following code.
void setName(char input[256]){
strncpy(name,input,sizeof(name));
}
You did not provide the class name. This is therefore declaring a static function, and not a class member. You also forgot to do this on your getNode function.
You left out the template statement.
You put a template implementation in a cpp file. Be aware that you cannot compile the cpp file as an object -- it must be included in a header, or you can ditch the file altogether and move your implementation into your header.

C++ does not name a type

creating some old data structures in C++. Currently I am having an issue with a doubly-linked list class:
List.h:
template <class T>
class List{
private:
int size;
struct listNode{
T data;
listNode* next;
listNode* prev;
listNode(T newData);
};
listNode * head;
listNode * tail;
listNode * curr;
listNode * find(listNode * place, int k);
void removeCurrent(listNode * temp);
public:
List();
int getSize() const;
void insert(int loc, T data);
void remove(int loc);
T const & getItem(int loc) const;
void print();
};
List.cpp:
#include "List.h"
#include <iostream>
using namespace std;
template<class T>
List<T>::List(){
size = 0;
head->next = tail;
head->prev = NULL;
tail->prev = head;
tail->next = NULL;
}
// getSize: public method that returns the size of the list
template<class T>
int List<T>::getSize() const {
return size;
}
// insert: public method that inserts data into the list
template<class T>
void List<T>::insert(int loc, T data){
if(loc <1){
cout<<"Invalid Location"<<endl;
return;
}
curr = find(head,loc-1);
listNode * newNode = new listNode(data);
newNode->next = curr->next;
newNode->prev = curr;
newNode->next->prev = newNode;
curr->next = newNode;
size++;
}
// remove: public method that inserts data into the list
template<class T>
void List<T>::remove(int loc){
if(loc <1){
cout<<"Invalid Location"<<endl;
return;
}
curr = find(head,loc); // Find the node infront of the target
removeCurrent(curr); // Remove that node
}
// removeCurrent: helper function that removes the current node
template<class T>
void List<T>::removeCurrent(listNode* temp){
listNode* t = temp->next;
temp->data = t->data; // HACK: take data from next node
temp->next = t->next;
t->next->prev = temp;
delete t;
t=NULL;
size--;
}
// find: private helper function that returns a pointer to the k-1 node
template<class T>
listNode * List<T>::find(listNode * place, int k){
if((k==0) || (place==NULL))
return place;
else return find(place->next,k-1);
}
// getItem: returns data at location loc
template<class T>
T const& List<T>::getItem(int loc) const{
curr = find(head,loc);
return curr->data;
}
// print: prints the sequence of variables in the list
template<class T>
void List<T>::print()
{
curr = head;
while(curr->next != tail){
curr = curr->next;
cout<<curr->data<<endl;
}
}
//listNode constructor
template<class T>
List<T>::listNode::listNode(T newdata):data(newdata),next(NULL),prev(NULL)
{}
The error I'm getting is the following:
error: 'listNode' does not name a type.
I have tried different suggestions offered in similar troubleshooting posts, but I'm still getting this error. I have a main.cpp that includes List.cpp, but it's practically empty.
You're going to have to specify which listNode you're talking about at the find method's return type because you defined it as a member of the List class and you're also going to have to use typename (because List<T> is a dependent scope).
template <class T>
typename List<T>::listNode* List<T>::find(listNode* place, int k)
{
if ((k == 0) || (place == NULL))
return place;
else
return find(place->next, k-1);
}
Assuming you're using c++11, you may also want to use nullptr instead of NULL since its safer and use the initializer list at the List constructor.

Why is Visual Studio forcing StackNode two different ways?

So I wrote a Linked List based implementation of the Stack ADT recently. However, I'm not quite sure why there is a bit of a discrepancy between how the nodes of the Stack are being declared. The compiler gets very angry and won't compile until I write them a certain way for certain functions. I'm extremely curious as to why this is the case.
Here are two different methods which the compiler wants two different formats.
Here is my destructor where the compiler wants StackNode *temp.
template <typename DataType>
StackLinked<DataType>::~StackLinked() {
StackNode *temp;
while (top != 0) {
temp = top;
top = top->next;
delete temp;
}
}
Here is my assignment operator overload where the compiler wants StackNode<DataType> *temp.
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other) {
if (this != &other) {
StackNode<DataType> *newNode, *current, *last;
if (top != 0) {
StackNode<DataType> *temp;
while (top != 0) {
temp = top;
top -> top->next;
delete temp;
}
}
if (other.top == 0) {
top = 0;
}
else {
current = other.top;
top = new StackNode<DataType>;
top->dataItem = current->dataItem;
top->next = 0;
last = top;
current = current->next;
while (current != 0) {
newNode = new StackNode<DataType>;
newNode->dataItem = current->dataItem;
newNode->next = 0;
last-> next = newNode;
last = newNode;
current = current->next;
}
}
}
return *this;
}
I don't know why this is, but the unknown is bothering me.
Note: My StackNode class is an inner class of the StackLinked class.
EDIT: Class declaration:
#ifndef STACKARRAY_H
#define STACKARRAY_H
#include <stdexcept>
#include <iostream>
using namespace std;
#include "Stack.h"
template <typename DataType>
class StackLinked : public Stack<DataType> {
public:
StackLinked(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
StackLinked(const StackLinked& other);
StackLinked& operator=(const StackLinked& other);
~StackLinked();
void push(const DataType& newDataItem) throw (logic_error);
DataType pop() throw (logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void showStructure() const;
private:
class StackNode {
public:
StackNode(const DataType& nodeData, StackNode* nextPtr);
DataType dataItem;
StackNode* next;
};
StackNode* top;
};
#endif
If any other details are needed. Just ask! Thank you for your time!
From the code you showed, StackNode<DataType> is not correct, because StackNode is not a class template.
This makes me think you have a template also named StackNode that the compiler is finding. Go check whether any of your files contain another version of StackNode.

help with linked list template

I need some help with my linked list. I think the problem is in the copy constructor or the assignment overload. it keeps giving my segmentation fault when i call:
(Queue test_int_copy(test_int);)
Any other error or poor implementation that you can see would also be very helpful.
the .h file
#ifndef QUEUE_H
#define QUEUE_H
template <class Object>
class Queue
{
public:
Queue();
//initializes empty queue
Queue(const Queue& a_queue);
//copy constructor
Queue& operator =(const Queue& rhs);
//overload assignment operator
bool enqueue(const Object& d);
//insert object into queue,return true
//return false if not
bool dequeue(Object& d);
//remove object from queue,return true
//if empty return false
bool isEmpty();
//check if empty
~Queue();
//destructor
private:
struct ListNode
{
Object obj;
//object that is in the list
ListNode *next;
//pointer to the next node
};
//struct of list
ListNode *head;
//pointer that points to head
};
#endif //Queue_H
#include "queue.cpp" //include queue.cpp with file
the .cpp file
#include <iostream>
using namespace std;
template <class Object>
Queue<Object>::Queue()
{
head = NULL;
}
template <class Object>
Queue<Object>::Queue(const Queue<Object> &a_queue)
{
head = NULL;
ListNode *nodePtr = a_queue.head;
while (nodePtr){
enqueue(nodePtr->obj);
nodePtr = nodePtr->next;
}
}
template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
//head = NULL;
ListNode *nodePtr = rhs.head;
Object temp;
while(head){
dequeue(temp);
}
while (nodePtr){
enqueue(nodePtr->obj);
nodePtr = nodePtr->next;
}
}
template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
ListNode *newNode = new ListNode;
newNode->obj = d;
newNode->next = NULL;
ListNode *nodePtr = NULL;
ListNode *previousNode = NULL;
if(isEmpty()){
head = newNode;
return true;
}
else{
nodePtr = head;
while(nodePtr != NULL){
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if(previousNode->next == NULL){
previousNode->next = newNode;
return true;
}
else
return false;
}
}
template <class Object>
bool Queue<Object>::dequeue (Object& d) //Dequeue
{
ListNode *nodePtr;
if(!head)
return false;
else{
if(head->next != NULL){
nodePtr = head;
d = nodePtr->obj;
head = head->next;
delete nodePtr;
return true;
}
else{
d = head->obj;
head = NULL;
return true;
}
}
}
template <class Object>
bool Queue<Object>::isEmpty() //Check if Empty
{
if(!head)
return true;
else
return false;
}
template <class Object>
Queue<Object>::~Queue() //Destructor
{
Object temp;
while(head)
dequeue (temp);
}
Also don't include the source file like that. Or if you insist, make sure to put it inside the include guard. e.g.
#include "queue.cpp" //include queue.cpp with file
#endif //Queue_H
Otherwise if the header is included multiple times (which happens all the time), you'll have a compilation error. (STuff will be defined multipled times).
Also, if you're going to include the "source" from the header -- make sure all functions are "inline" (implicitly or explicitly), otherwise you'll get linker errors
You should initialize your head pointer in your copy constructor (and also in your operator = function by the way). If you don't do so, head might end up with an undefined value and fail the IsEmpty test, witch may lead to a crash in enqeue.
One mistake I can see: in the dequeue() method, you do not set head to NULL after removing the last item.
Also, in the assignment overload:
while(head){
dequeue(temp);
head = head->next;
}
... You do not need to set head = head->next, that has already been done by the dequeue() method.
Your enqueue() function always inserts a node in the end. So it's better to maintain an extra member variable as: ListNode *tail;pointing to the last node of the list. So when you are adding a node you don't need to traverse through whole list and simply update the *tail. This will save lot of time.
For whatsoever reason you return false from enqueu(), do NOT forget to delete newNode; otherwise it will leak memory.
isEmpty() you can simplify as bool isEmpty() { return (0 != head); }.
Instead of declaring & passing a raw object in dequeue(), you can simply return Object* if element is removed or return 0 (NULL) if it's empty; that's equivalent to true and false.
Importantly, whenever you delete head; always do head = 0;. Because that's a member variable and should not be dangling.