jeudi 5 mars 2015

When I try to call a method from another class I get an "Error: identifier is undefined"


I have two classes, LinkedList and HashTable, and when I try to call a method from LinkedList, specifically insertListElement(), in the insertElement() method of Class HashTable, it gives a compilation error: "Error: identifier insertListElement is undefined" How do I successfully call this method?


Here is my code:



// Assignment #4
// Name: Dylan Rich
// ASU Email Address: drich2@asu.edu
// Description: To be completed

#include "stdafx.h"
#include <string> //to use strings


using namespace std;

int main()
{
return 0;
}

//class LinkedList will contains a linked list of courses
class LinkedList
{
private:
LinkedList *next;
public:
string courseNum; //the number code of the course
string courseTitle; //the title of the course
LinkedList(); //constructor
~LinkedList(); //destructor
void setStrings(string num, string title); //sets the data for the node
bool insertListElement(LinkedList *oldHead, LinkedList *newHead); //inserts an element at the beginning of the list
LinkedList *searchListElement(LinkedList *toSearch, LinkedList *head); //searches for an element and returns it
bool deleteListElement(LinkedList *head, LinkedList *toDelete); //deletes an element at the list, returns false if no such element
void setNext(LinkedList *nextNode); //sets a specific node as next
LinkedList *getNext(); //retrieves the next node
};

//Constructor
LinkedList::LinkedList()
{

}

//Deconstructor
LinkedList::~LinkedList()
{
delete next;
next = NULL;
}

//sets the data for the node
void LinkedList::setStrings(string num, string title)
{
courseNum = num;
courseTitle = title;
next = NULL;
}

//inserts an element at the beginning of the list
bool LinkedList::insertListElement(LinkedList *oldHead, LinkedList *newHead)
{
if (oldHead == NULL) //if list is empty, it's automatically the head and returns false
return false;
newHead->setNext(oldHead); //otherwise it becomes the new head and returns true
return true;
}

//searches for an element and returns it
LinkedList *LinkedList::searchListElement(LinkedList *toSearch, LinkedList *head)
{
LinkedList *temp = NULL; //creates a temporary node
if (head == NULL) //if list is empty return false
return temp;
while (head->getNext() != NULL)//while the list still has elements
{
if ((head->courseNum.compare(toSearch->courseNum)) == 0 && (head- >courseTitle.compare(toSearch->courseTitle)))//if this is the one we're looking for, return it
return head;
head = head->getNext(); //move to the next element in the list
}
return temp;
}

//Delete an element from the list
bool LinkedList::deleteListElement(LinkedList *head, LinkedList *toDelete)
{
LinkedList *temp = NULL; //creates a temporary node
if (head == NULL) //if list is empty return false
return false;
do
{
//if this is the one we're looking for
if ((head->courseNum.compare(toDelete->courseNum)) == 0 && (head->courseTitle.compare(toDelete->courseTitle)))
{
temp->setNext(head->getNext());
head->~LinkedList();
return true;
}
temp = head;
if (head->getNext() != NULL)
head = head->getNext();
} while (head->getNext() != NULL);
return false;
}

//Sets a specfic node as next
void LinkedList::setNext(LinkedList *nextNode)
{
next = nextNode;
}

//shows which node next is pointing to
LinkedList * LinkedList::getNext(void)
{
return next;
}
//End class LinkedList



//class HashTable contains a hash table of linked lists of courses
class HashTable
{
public:
LinkedList ** hash; //hash table
HashTable(int length); //constructor
int h(LinkedList *element, int hashSize); //hash function
bool insertElement(int key, LinkedList *element); //inserts an element at a key
LinkedList *searchElement(LinkedList *element); //searches for and returns an element
bool deleteElement(LinkedList *element); //deletes an element
};

//Constructor
HashTable::HashTable(int length)
{
hash = new LinkedList* [length];
}

//Hash function, the key is the sum of the numbers making up the course code
int HashTable::h(LinkedList *element, int hashSize)
{
int a, b, c, key;
a = element->courseNum.at(3);
b = element->courseNum.at(4);
c = element->courseNum.at(5);
key = (a + b + c) % hashSize;
return key;
}

//inserts an element at a specific key, returns true if successful
bool HashTable::insertElement(int key, LinkedList *element)
{
LinkedList *temp = hash[key];
bool joined = insertListElement(temp, element);
hash[key] = element;strong text
}


Most of it is irrelevant except the two methods I mentioned earlier.




Aucun commentaire:

Enregistrer un commentaire