Homework
I'm trying to call two functions in my main function from each of my three header files.
#ifndef UNSORTEDPQ_H #define UNSORTEDPQ_H #include <exception> #include <cstddef> #include <cmath> using namespace std; template <class Type> class UnsortedPQ { private: struct DLLNode { Type data; DLLNode *prev; DLLNode *next; }; DLLNode *head, *tail, *node; public: void insertUPQ ( Type data ){ DLLNode *newnode = new DLLNode; newnode -> node = data; newnode -> prev = NULL; if (isEmpty()){ newnode -> next = NULL; head = tail = newnode; } else { newnode -> next = NULL; newnode -> prev = node -> prev -> next; tail = newnode; } } // removes and returns the minimum value in the queue // throws an exception if the queue is empty Type removeUPQ ( void ) throw(exception){ if (isEmpty()) { exception("Can't dequeue empty Priority Queue"); } Type min = head -> data; // special case: queue has one element if (size()==1){ while (head != NULL) { DLLNode *next = head -> next; delete head; head = next; } return min; } // determine minimum element for (node = head; node != NULL; node = node -> next){ if (node -> data < min) { min = node -> data; } } // special case: queue has two elements if (size()==2){ if (head -> data== min){ delete head; tail -> previous = NULL; head = tail; } else { delete tail; head -> next = NULL; tail = head; } return min; } // remove element and update next and previous for elements around it for (node = head; node != NULL; node = node -> next){ if (node -> data == min){ // special case if min elem is head if (node == head){ DLLNode*oldHead=head; head = head -> next; head -> prev = NULL; delete oldHead; return min; } // special case if min elem is tail if(node == tail){ DLLNode*oldTail=tail; tail = tail-> prev; tail -> next = NULL; delete oldTail; return min; } // for cases where min elem is not head or tail DLLNode *prevNode = node -> prev; DLLNode *nextNode = node -> next; prevNode -> next = node -> next; nextNode -> prev = node -> prev; delete node; return min; } } #ifndef SORTEDPQ_H #define SORTEDPQ_H #include <exception> #include <cstddef> #include <cmath> using namespace std; template <class Type> class SortedPQ { private: struct DLLNode { Type data; DLLNode *prev; DLLNode *next; }; DLLNode *head, *tail, *node; public: // inserts a piece of data into the priority queue void insertSPQ ( Type data ){ DLLNode curr = head; while(curr != tail && node -> data <= curr -> data){ curr = curr -> next; } DLLNode tempCurr = curr; node -> next = tempCurr; node -> prev = tempCurr -> prev; tempCurr -> prev -> next = node; tempCurr -> prev = node; } // removes and returns the minimum value in the queue // throws an exception if the queue is empty Type removeSPQ ( void ) throw(exception){ if (isEmpty()) { exception("Can't dequeue an empty Priority Queue"); } Type min = head -> node; if (node == head){ DLLNode*oldHead=head; head = head -> next; head -> prev = NULL; delete oldHead; return min; } #ifndef HEAPPQ_H #define HEAPPQ_H #include <exception> #include <cstddef> #include <cmath> using namespace std; template <class Type> class HeapPQ { private: Type *data; Type heapSize; Type arraySize; Type floor(Type arg); Type getLChildIndex(Type nodeIndex){ return 2 * nodeIndex + 1; } Type getRightChildIndex(Type nodeIndex){ return 2 * nodeIndex + 2; } Type getParentIndex(Type nodeIndex){ return floor((nodeIndex - 1) / 2); } public: void HeapPropertyInsert(Type nodeIndex){ Type parentIndex, tmp; if(nodeIndex != 0){ parentIndex = getParentIndex(nodeIndex); if(data[parentIndex] > data[nodeIndex]){ tmp = data[parentIndex]; data[parentIndex] = data[nodeIndex]; data[nodeIndex] = tmp; HeapPropertyInsert(parentIndex); } } } // inserts a piece of data into the priority queue void insertHPQ ( Type data ) throw(exception){ if (heapSize == arraySize){ exception ("Cannot insert, heap is full"); } else{ heapSize++; data[heapSize - 1] = data; HeapPropertyInsert(heapSize - 1); } } void HeapPropertyRemove(Type nodeIndex){ Type leftChildIndex, rightChildIndex, minIndex, tmp; leftChildIndex = getLChildIndex(nodeIndex); rightChildIndex = getRightChildIndex(nodeIndex); if(rightChildIndex >= heapSize){ if(leftChildIndex >= heapSize) return; else minIndex = leftChildIndex; } else{ if (data[leftChildIndex] < data[rightChildIndex]) minIndex = leftChildIndex; else minIndex = rightChildIndex; } if(data[nodeIndex] > data[minIndex]){ tmp = data[minIndex]; data[minIndex] = data[nodeIndex]; data[nodeIndex] = tmp; HeapPropertyRemove(minIndex); } } // removes and returns the minimum value in the queue // throws an exception if the queue is empty Type removeHPQ ( void ) throw(exception){ if(isEmpty()) exception("Cannot remove, heap is empty"); else{ data[0] = data[heapSize - 1]; heapSize--; if(heapSize > 0) HeapPropertyRemove(0); } } My main.cpp:
#include "UnsortedPQ.h" #include "SortedPQ.h" #include "HeapPQ.h" #include <iostream> #include <fstream> #include <cstring> #include <cstdlib> using namespace std; int main(){ int choice; int elem; int array_size = 1024; char * array = new char[array_size]; int position = 0; ifstream fin("numbers.txt"); if(fin.is_open()){ cout << "File opened successfully." << endl; while(!fin.eof() && position < array_size){ fin.get(array[position]); position++; } array[0] = choice; switch (choice){ case '0': //UnsortedPQ for(int i = 2; array[i]; i++ ){ insertUPQ(array[i]); <----- error removeUPQ(array[i]); <----- error } break; case '1': //SortedPQ for(int i = 2; array[i]; i++ ){ insertSPQ(array[i]); <----- error removeSPQ(array[i]); <----- error } break; case '2':// HeapPQ for(int i = 2; array[i]; i++ ){ insertHPQ(array[i]); <----- error removeHPQ(array[i]); <----- error } break; } } else{ cout << "File could not be opened." << endl; } return 0; } I keep receiving compiler errors stating that these functions "are not declared in this scope". I thought that since the header files were included and the functions are public, that I could call them at will anywhere in the main.cpp. Again, this is homework, so I'm not looking for a quick fix, but some advice or a point in the right direction would be much appreciated.
#endifat the end of all of your header files.insertSPQ, for example, is inside theSortedPQclass. It's a method. That means you can only call it on instances of the class. And you seem to have a randompublic:elsewhere outside of a class declaration (it's only valid within a class/struct).