Obecnie mam binarne drzewo konfiguracji wyszukiwania, wykorzystując szablony pozwalają mi łatwo zmienić typ danych w wyszukiwaniu binarnym drzewie. W tej chwili mam problemy przeciążenia klasę studentRecord który zawiera dane mają być przechowywane w drzewie. Muszę przeciążać operatorów porównania w tej klasie, tak, że moja BST można właściwie porównać dwa obiekty na podstawie jednego z ich zawartością (w tym przypadku, student ID). Jednak pomimo przeciążania operatorów ciągu studentRecord, odpowiednie porównania są nadal nie występujące.
Szczegóły poniżej:
Na chwilę obecną studentTree BST obiekt został utworzony, typu
bst<studentRecord *> studentTree;
studentRecord jest następujące klasy:
// studentRecord class
class studentRecord{
public:
// standard constructors and destructors
studentRecord(int studentID, string lastName, string firstName, string academicYear){ // constructor
this->studentID=studentID;
this->lastName=lastName;
this->firstName=firstName;
this->academicYear=academicYear;
}
friend bool operator > (studentRecord &record1, studentRecord &record2){
if (record1.studentID > record2.studentID)
cout << Greater! << endl;
else
cout << Less then! << endl;
return (record1.studentID > record2.studentID);
}
private:
// student information
string studentID;
string lastName;
string firstName;
string academicYear;
};
Ilekroć nowe elementy są dodawane do mojego BST, muszą być ze sobą porównywane. Dlatego chciałem przeciążać klasę studentRecord, tak, że kiedy pojawia się ten proces porównania studentIDs porównywane są (jak inaczej, nieważne porównanie zostanie wykonane).
Jednak nie moja funkcja wstawiania wykorzystuje moje przeciążonych funkcji porównawczych. Zamiast tego wydaje się być porównanie dwóch obiektów w jakiś inny sposób, w wyniku nieprawidłowej segregacji w BST. Część mojej funkcji insert jest poniżej - ważne jest, aby pamiętać, że zarówno toInsert i nodePtr-> Dane powinny być typu studentRecord, ze względu na występujące procesowego szablonów.
// insert (private recursive function)
template<typename bstType>
void bst<bstType>::insert(bstType & toInsert, bstNodePtr & nodePtr){
// check to see if the nodePtr is null, if it is, we've found our insertion point (base case)
if (nodePtr == NULL){
nodePtr = new bst<bstType>::bstNode(toInsert);
}
// else, we are going to need to keep searching (recursive case)
// we perform this operation recursively, to allow for rotations (if AVL tree support is enabled)
// check for left
else if (toInsert < (nodePtr->data)){ // go to the left (item is smaller)
// perform recursive insert
insert(toInsert,nodePtr->left);
// AVL tree sorting
if(getNodeHeight(nodePtr->left) - getNodeHeight(nodePtr->right) == 2 && AVLEnabled)
if (toInsert < nodePtr->left->data)
rotateWithLeftChild(nodePtr);
else
doubleRotateWithLeftChild(nodePtr);
}
Również tutaj jest część umowach definicja klasy BST
// BST class w/ templates
template <typename bstType>
class bst{
private: // private data members
// BST node structure (inline class)
class bstNode{
public: // public components in bstNode
// data members
bstType data;
bstNode* left;
bstNode* right;
// balancing information
int height;
// constructor
bstNode(bstType item){
left = NULL;
right = NULL;
data = item;
height = 0;
}
// destructor
// no special destructor is required for bstNode
};
// BST node pointer
typedef bstNode* bstNodePtr;
public: // public functions.....
Wszelkie pomysły na to, co może być przyczyną? Ja przeciążenia niewłaściwą klasę lub niewłaściwą funkcję? Każda pomoc jest mile widziana - Wydaje mi się zgubić, ponieważ tak wiele różnych rzeczy występują jednocześnie.













