0

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.

3
  • 1
    You are missing #endif at the end of all of your header files. Commented Mar 19, 2015 at 19:40
  • 3
    Owww... Too much source here. We're not solving homeworks. Commented Mar 19, 2015 at 19:41
  • 1
    insertSPQ, for example, is inside the SortedPQ class. It's a method. That means you can only call it on instances of the class. And you seem to have a random public: elsewhere outside of a class declaration (it's only valid within a class/struct). Commented Mar 19, 2015 at 19:45

2 Answers 2

2

All the functions you are trying to call are methods that belong to the specific class. In order to use these method you have to create an object of a specific class:

char * array = new char[array_size]; UnsortedPQ<char> Object; Object.insertUPQ(array[i]); 

There is a link to the tutorial that explains what exactly are classes in C++, how to create them and use them:

http://www.cplusplus.com/doc/tutorial/classes/

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't I have to use <Type> since my classes are templated?
You mean on each method call? No. You tell the compiler with what class your class is compiled when declaring an object of a templated class: UnsortedPQ<char> Object; In this situation <Type> is <char> and all methods and variables in the class are compiled as chars. This way you can create a class that can work with many data types and even with other classes.
0

There are a lot of issues with this code. Without even going into possible problem with the individual function implementations, the following errors need to be fixed:

  1. You can't call a class method without having an instance of that class. So, to call the method insertSPQ, you have to have SortedPQ class. You can create one like this:

SortedPQ<char> *spq = new SortedPQ<char>;

Then call the function like this:

spq->insertSPQ(array[i]);


  1. You are calling functions using an incorrect signature. For example, you try to call

spq->removeSPQ(array[i]);

(after I added the spq class instance ), but the arguments for that function are defined as void as shown below:

Type removeSPQ(void) throw(exception){

So your sorted if condition should look more like:

 case '1': //SortedPQ SortedPQ<char> *spq = new SortedPQ<char>; for (int i = 2; array[i]; i++){ spq->insertSPQ(array[i]); spq->removeSPQ(array[i]); } break; 

Of course, that doesn't really fix everything, because you still need to free the memory from spq.

Additonally, as another commenter pointed out, you are missing #endif 's. Those should be the last line of each header file to close your initial #ifndef 's.

Finally, it's usually considered a bad practice to have your class implementation and declaration in the same header file. It's generally better to have a .h file for the declaration and a separate .cpp for the actual implementation. It should WORK as is, just not a best practice.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.